Checkbox and Select validation

pull/6632/head
tugcekucukoglu 2024-10-21 17:45:53 +03:00
parent b0f0e1cbb8
commit 451a7c88df
4 changed files with 233 additions and 0 deletions

View File

@ -0,0 +1,106 @@
<template>
<DocSectionText v-bind="$attrs"> </DocSectionText>
<div class="card flex justify-center">
<Form v-slot="$form" :resolver="resolver" :defaultValues="defaultValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
<div class="flex flex-col gap-2">
<CheckboxGroup name="checkbox" class="flex flex-wrap gap-4">
<div class="flex items-center">
<Checkbox inputId="ingredient1" value="Cheese" />
<label for="ingredient1" class="ml-2"> Cheese </label>
</div>
<div class="flex items-center">
<Checkbox inputId="ingredient2" value="Mushroom" />
<label for="ingredient2" class="ml-2"> Mushroom </label>
</div>
<div class="flex items-center">
<Checkbox inputId="ingredient3" value="Pepper" />
<label for="ingredient3" class="ml-2"> Pepper </label>
</div>
<div class="flex items-center">
<Checkbox inputId="ingredient4" value="Onion" />
<label for="ingredient4" class="ml-2"> Onion </label>
</div>
</CheckboxGroup>
<Message v-if="$form.checkbox?.invalid" severity="error">{{ $form.checkbox.errors[0]?.message }}</Message>
</div>
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button>
</Form>
</div>
<DocSectionCode :code="code" />
</template>
<script>
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
export default {
data() {
return {
defaultValues: {
checkbox: []
},
resolver: null,
schema: z.object({
checkbox: z.array(z.string()).min(1, { message: 'At least one checkbox must be selected.' })
}),
code: {
basic: `
<Form v-slot="$form" :resolver="resolver" :defaultValues="defaultValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
<div class="flex flex-col gap-2">
<InputText name="username" type="text" placeholder="Username" />
<Message v-if="$form.username?.invalid" severity="error">{{ $form.username.errors[0]?.message }}</Message>
</div>
<div class="flex flex-col gap-2">
<InputText name="email" type="text" placeholder="Email" />
<Message v-if="$form.email?.invalid" severity="error">{{ $form.email.errors[0]?.message }}</Message>
</div>
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button>
</Form>
`,
options: `
<template>
<div class="card flex justify-center">
<InputText type="text" v-model="value" />
</div>
</template>
<script>
export default {
data() {
return {
value: null
}
}
}
<\/script>
`,
composition: `
<template>
<div class="card flex justify-center">
<InputText type="text" v-model="value" />
</div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(null);
<\/script>
`
}
};
},
mounted() {
this.resolver = zodResolver(this.schema);
},
methods: {
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>

View File

@ -0,0 +1,115 @@
<template>
<DocSectionText v-bind="$attrs"> </DocSectionText>
<div class="card flex justify-center">
<Form v-slot="$form" :resolver="resolver" :defaultValues="defaultValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
<div class="flex flex-col gap-2">
<Select name="city" :options="cities" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.errors[0]?.message }}</Message>
</div>
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button>
</Form>
</div>
<DocSectionCode :code="code" />
</template>
<script>
import { CountryService } from '@/service/CountryService';
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
export default {
data() {
return {
defaultValues: {
city: { name: '' }
},
cities: [
{ name: 'New York', code: 'NY' },
{ name: 'Rome', code: 'RM' },
{ name: 'London', code: 'LDN' },
{ name: 'Istanbul', code: 'IST' },
{ name: 'Paris', code: 'PRS' }
],
resolver: null,
schema: z.object({
city: z.object({
name: z
.string()
.min(1, 'City option cannot be empty.')
.refine((val) => val !== '', {
message: 'City should be selected.'
})
})
}),
code: {
basic: `
<Form v-slot="$form" :resolver="resolver" :defaultValues="defaultValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
<div class="flex flex-col gap-2">
<InputText name="username" type="text" placeholder="Username" />
<Message v-if="$form.username?.invalid" severity="error">{{ $form.username.errors[0]?.message }}</Message>
</div>
<div class="flex flex-col gap-2">
<InputText name="email" type="text" placeholder="Email" />
<Message v-if="$form.email?.invalid" severity="error">{{ $form.email.errors[0]?.message }}</Message>
</div>
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button>
</Form>
`,
options: `
<template>
<div class="card flex justify-center">
<InputText type="text" v-model="value" />
</div>
</template>
<script>
export default {
data() {
return {
value: null
}
}
}
<\/script>
`,
composition: `
<template>
<div class="card flex justify-center">
<InputText type="text" v-model="value" />
</div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(null);
<\/script>
`
}
};
},
mounted() {
CountryService.getCountries().then((data) => (this.countries = data));
this.resolver = zodResolver(this.schema);
},
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 }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>

View File

@ -16,6 +16,7 @@ import BasicDoc from '@/doc/checkbox/BasicDoc.vue';
import DisabledDoc from '@/doc/checkbox/DisabledDoc.vue'; import DisabledDoc from '@/doc/checkbox/DisabledDoc.vue';
import DynamicDoc from '@/doc/checkbox/DynamicDoc.vue'; import DynamicDoc from '@/doc/checkbox/DynamicDoc.vue';
import FilledDoc from '@/doc/checkbox/FilledDoc.vue'; import FilledDoc from '@/doc/checkbox/FilledDoc.vue';
import FormDoc from '@/doc/checkbox/FormDoc.vue';
import GroupDoc from '@/doc/checkbox/GroupDoc.vue'; import GroupDoc from '@/doc/checkbox/GroupDoc.vue';
import ImportDoc from '@/doc/checkbox/ImportDoc.vue'; import ImportDoc from '@/doc/checkbox/ImportDoc.vue';
import IndeterminateDoc from '@/doc/checkbox/IndeterminateDoc.vue'; import IndeterminateDoc from '@/doc/checkbox/IndeterminateDoc.vue';
@ -37,6 +38,11 @@ export default {
label: 'Basic', label: 'Basic',
component: BasicDoc component: BasicDoc
}, },
{
id: 'form',
label: 'Form',
component: FormDoc
},
{ {
id: 'indeterminate', id: 'indeterminate',
label: 'Indeterminate', label: 'Indeterminate',

View File

@ -12,6 +12,7 @@ import EditableDoc from '@/doc/select/EditableDoc.vue';
import FilledDoc from '@/doc/select/FilledDoc.vue'; import FilledDoc from '@/doc/select/FilledDoc.vue';
import FilterDoc from '@/doc/select/FilterDoc.vue'; import FilterDoc from '@/doc/select/FilterDoc.vue';
import FloatLabelDoc from '@/doc/select/FloatLabelDoc.vue'; import FloatLabelDoc from '@/doc/select/FloatLabelDoc.vue';
import FormDoc from '@/doc/select/FormDoc.vue';
import GroupDoc from '@/doc/select/GroupDoc.vue'; import GroupDoc from '@/doc/select/GroupDoc.vue';
import IftaLabelDoc from '@/doc/select/IftaLabelDoc.vue'; import IftaLabelDoc from '@/doc/select/IftaLabelDoc.vue';
import ImportDoc from '@/doc/select/ImportDoc.vue'; import ImportDoc from '@/doc/select/ImportDoc.vue';
@ -37,6 +38,11 @@ export default {
label: 'Basic', label: 'Basic',
component: BasicDoc component: BasicDoc
}, },
{
id: 'form',
label: 'Form',
component: FormDoc
},
{ {
id: 'checkmark', id: 'checkmark',
label: 'Checkmark', label: 'Checkmark',