Select form updates
parent
2031423af8
commit
48b444257e
|
@ -1,19 +1,18 @@
|
||||||
<template>
|
<template>
|
||||||
<DocSectionText v-bind="$attrs"> </DocSectionText>
|
<DocSectionText v-bind="$attrs"> </DocSectionText>
|
||||||
<div class="card flex justify-center">
|
<div class="card flex justify-center">
|
||||||
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full md:w-56">
|
||||||
<div class="flex flex-col gap-2">
|
<div class="flex flex-col gap-2">
|
||||||
<Select name="city" :options="cities" optionLabel="name" placeholder="Select a City" fluid />
|
<Select name="city" :options="cities" showClear optionLabel="name" placeholder="Select a City" fluid />
|
||||||
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.errors[0]?.message }}</Message>
|
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.errors[0]?.message }}</Message>
|
||||||
</div>
|
</div>
|
||||||
<Button type="submit" severity="secondary" label="Submit" />
|
<Button type="submit" severity="secondary" label="Submit" />
|
||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
<DocSectionCode :code="code" />
|
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { CountryService } from '@/service/CountryService';
|
|
||||||
import { zodResolver } from '@primevue/form/resolvers';
|
import { zodResolver } from '@primevue/form/resolvers';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
@ -23,6 +22,16 @@ export default {
|
||||||
initialValues: {
|
initialValues: {
|
||||||
city: { name: '' }
|
city: { name: '' }
|
||||||
},
|
},
|
||||||
|
resolver: zodResolver(
|
||||||
|
z.object({
|
||||||
|
city: z.union([
|
||||||
|
z.object({
|
||||||
|
name: z.string().min(1, 'City required.')
|
||||||
|
}),
|
||||||
|
z.any().refine((val) => false, { message: 'City required.' })
|
||||||
|
])
|
||||||
|
})
|
||||||
|
),
|
||||||
cities: [
|
cities: [
|
||||||
{ name: 'New York', code: 'NY' },
|
{ name: 'New York', code: 'NY' },
|
||||||
{ name: 'Rome', code: 'RM' },
|
{ name: 'Rome', code: 'RM' },
|
||||||
|
@ -30,72 +39,120 @@ export default {
|
||||||
{ name: 'Istanbul', code: 'IST' },
|
{ name: 'Istanbul', code: 'IST' },
|
||||||
{ name: 'Paris', code: 'PRS' }
|
{ name: 'Paris', code: 'PRS' }
|
||||||
],
|
],
|
||||||
resolver: null,
|
|
||||||
schema: z.object({
|
|
||||||
city: z.object({
|
|
||||||
name: z.string().min(1, 'City is required.')
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
code: {
|
code: {
|
||||||
basic: `
|
basic: `
|
||||||
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full md:w-56">
|
||||||
<div class="flex flex-col gap-2">
|
<div class="flex flex-col gap-2">
|
||||||
<Select name="city" :options="cities" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
|
<Select name="city" :options="cities" optionLabel="name" placeholder="Select a City" fluid />
|
||||||
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.errors[0]?.message }}</Message>
|
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.errors[0]?.message }}</Message>
|
||||||
</div>
|
</div>
|
||||||
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button>
|
<Button type="submit" severity="secondary" label="Submit" />
|
||||||
</Form>
|
</Form>
|
||||||
`,
|
`,
|
||||||
options: `
|
options: `
|
||||||
<template>
|
<template>
|
||||||
<div class="card flex justify-center">
|
<div class="card flex justify-center">
|
||||||
<InputText type="text" v-model="value" />
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full md:w-56">
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
<Select name="city" :options="cities" optionLabel="name" placeholder="Select a City" fluid />
|
||||||
|
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.errors[0]?.message }}</Message>
|
||||||
|
</div>
|
||||||
|
<Button type="submit" severity="secondary" label="Submit" />
|
||||||
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { zodResolver } from '@primevue/form/resolvers';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
value: null
|
initialValues: {
|
||||||
|
city: { name: '' }
|
||||||
|
},
|
||||||
|
resolver: zodResolver(
|
||||||
|
z.object({
|
||||||
|
city: z.union([
|
||||||
|
z.object({
|
||||||
|
name: z.string().min(1, 'City required.')
|
||||||
|
}),
|
||||||
|
z.any().refine((val) => false, { message: 'City required.' })
|
||||||
|
])
|
||||||
|
})
|
||||||
|
),
|
||||||
|
cities: [
|
||||||
|
{ name: 'New York', code: 'NY' },
|
||||||
|
{ name: 'Rome', code: 'RM' },
|
||||||
|
{ name: 'London', code: 'LDN' },
|
||||||
|
{ name: 'Istanbul', code: 'IST' },
|
||||||
|
{ name: 'Paris', code: 'PRS' }
|
||||||
|
],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
onFormSubmit({ valid }) {
|
||||||
|
if (valid) {
|
||||||
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
<\/script>
|
<\/script>
|
||||||
|
|
||||||
`,
|
`,
|
||||||
composition: `
|
composition: `
|
||||||
<template>
|
<template>
|
||||||
<div class="card flex justify-center">
|
<div class="card flex justify-center">
|
||||||
<InputText type="text" v-model="value" />
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full md:w-56">
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
<Select name="city" :options="cities" optionLabel="name" placeholder="Select a City" fluid />
|
||||||
|
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.errors[0]?.message }}</Message>
|
||||||
|
</div>
|
||||||
|
<Button type="submit" severity="secondary" label="Submit" />
|
||||||
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue';
|
||||||
|
import { zodResolver } from '@primevue/form/resolvers';
|
||||||
|
import { useToast } from "primevue/usetoast";
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
const value = ref(null);
|
const toast = useToast();
|
||||||
|
const initialValues = ref({
|
||||||
|
city: { name: '' }
|
||||||
|
});
|
||||||
|
const resolver = ref(zodResolver(
|
||||||
|
z.object({
|
||||||
|
city: z.union([
|
||||||
|
z.object({
|
||||||
|
name: z.string().min(1, 'City required.')
|
||||||
|
}),
|
||||||
|
z.any().refine((val) => false, { message: 'City required.' })
|
||||||
|
])
|
||||||
|
})
|
||||||
|
));
|
||||||
|
const cities = ref([
|
||||||
|
{ name: 'New York', code: 'NY' },
|
||||||
|
{ name: 'Rome', code: 'RM' },
|
||||||
|
{ name: 'London', code: 'LDN' },
|
||||||
|
{ name: 'Istanbul', code: 'IST' },
|
||||||
|
{ name: 'Paris', code: 'PRS' }
|
||||||
|
]);
|
||||||
|
|
||||||
|
const onFormSubmit = ({ valid }) => {
|
||||||
|
if (valid) {
|
||||||
|
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
||||||
|
}
|
||||||
|
};
|
||||||
<\/script>
|
<\/script>
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
mounted() {
|
|
||||||
CountryService.getCountries().then((data) => (this.countries = data));
|
|
||||||
this.resolver = zodResolver(this.schema);
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
search(event) {
|
|
||||||
setTimeout(() => {
|
|
||||||
if (!event.query.trim().length) {
|
|
||||||
this.filteredCountries = [...this.countries];
|
|
||||||
} else {
|
|
||||||
this.filteredCountries = this.countries.filter((country) => {
|
|
||||||
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, 250);
|
|
||||||
},
|
|
||||||
onFormSubmit({ valid }) {
|
onFormSubmit({ valid }) {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
||||||
|
|
Loading…
Reference in New Issue