primevue-mirror/apps/showcase/doc/forms/formfield/ResolverDoc.vue

248 lines
12 KiB
Vue
Raw Normal View History

2024-11-01 06:00:00 +00:00
<template>
<DocSectionText label="Resolver" :level="2" v-bind="$attrs">
<p>Each FormField can have its own dedicated resolver, allowing you to define custom validation logic for individual fields. This flexibility enables tailored validation rules, ensuring that each form field meets specific criteria.</p>
</DocSectionText>
<div class="card flex justify-center">
<Form :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-80">
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="username" initialValue="" :resolver="zodUserNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="Username" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="firstname" initialValue="" :resolver="yupFirstNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="First Name" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="lastname" initialValue="" :resolver="valibotLastNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="Last Name" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="password" initialValue="" :resolver="customPasswordResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<Password type="text" placeholder="Password" :feedback="false" toggleMask fluid />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="details" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<Textarea placeholder="Details" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8', yup: '1.4.0', valibot: '0.42.1' }" />
</template>
<script>
import { valibotResolver, yupResolver, zodResolver } from '@primevue/forms/resolvers';
import * as v from 'valibot';
import * as yup from 'yup';
import { z } from 'zod';
export default {
data() {
return {
initialValues: {
details: ''
},
resolver: zodResolver(
z.object({
details: z.string().min(1, { message: 'Details is required via Form Resolver.' })
})
),
zodUserNameResolver: zodResolver(z.string().min(1, { message: 'Username is required via Zod.' })),
yupFirstNameResolver: yupResolver(yup.string().required('First name is required via Yup.')),
valibotLastNameResolver: valibotResolver(v.pipe(v.string(), v.minLength(1, 'Last name is required via Valibot.'))),
code: {
basic: `
<Form :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-80">
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="username" initialValue="" :resolver="zodUserNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="Username" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="firstname" initialValue="" :resolver="yupFirstNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="First Name" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="lastname" initialValue="" :resolver="valibotLastNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="Last Name" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="password" initialValue="" :resolver="customPasswordResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<Password type="text" placeholder="Password" :feedback="false" toggleMask fluid />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="details" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<Textarea placeholder="Details" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
`,
options: `
<template>
<div class="card flex justify-center">
<Toast />
<Form :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-80">
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="username" initialValue="" :resolver="zodUserNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="Username" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="firstname" initialValue="" :resolver="yupFirstNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="First Name" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="lastname" initialValue="" :resolver="valibotLastNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="Last Name" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="password" initialValue="" :resolver="customPasswordResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<Password type="text" placeholder="Password" :feedback="false" toggleMask fluid />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="details" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<Textarea placeholder="Details" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
</template>
<script>
import { valibotResolver, yupResolver, zodResolver } from '@primevue/forms/resolvers';
import * as v from 'valibot';
import * as yup from 'yup';
import { z } from 'zod';
export default {
data() {
return {
initialValues: {
details: ''
},
resolver: zodResolver(
z.object({
details: z.string().min(1, { message: 'Details is required via Form Resolver.' })
})
),
zodUserNameResolver: zodResolver(z.string().min(1, { message: 'Username is required via Zod.' })),
yupFirstNameResolver: yupResolver(yup.string().required('First name is required via Yup.')),
valibotLastNameResolver: valibotResolver(v.pipe(v.string(), v.minLength(1, 'Last name is required via Valibot.')))
};
},
methods: {
customPasswordResolver: ({ value }) => {
const errors = [];
if (!value) {
errors.push({ message: 'Password is required via Custom.' });
}
return {
errors
};
},
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
<\/script>
`,
composition: `
<template>
<div class="card flex justify-center">
<Form :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-80">
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="username" initialValue="" :resolver="zodUserNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="Username" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="firstname" initialValue="" :resolver="yupFirstNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="First Name" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="lastname" initialValue="" :resolver="valibotLastNameResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<InputText type="text" placeholder="Last Name" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="password" initialValue="" :resolver="customPasswordResolver" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<Password type="text" placeholder="Password" :feedback="false" toggleMask fluid />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="details" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<Textarea placeholder="Details" />
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
</FormField>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
</template>
<script setup>
import { reactive } from 'vue';
import { valibotResolver, yupResolver, zodResolver } from '@primevue/forms/resolvers';
import * as v from 'valibot';
import * as yup from 'yup';
import { z } from 'zod';
import { useToast } from 'primevue/usetoast';
const toast = useToast();
const initialValues = reactive({
details: ''
});
const resolver = zodResolver(
z.object({
details: z.string().min(1, { message: 'Details is required via Form Resolver.' })
})
);
const zodUserNameResolver = zodResolver(z.string().min(1, { message: 'Username is required via Zod.' }));
const yupFirstNameResolver = yupResolver(yup.string().required('First name is required via Yup.'));
const valibotLastNameResolver = valibotResolver(v.pipe(v.string(), v.minLength(1, 'Last name is required via Valibot.')));
const customPasswordResolver = ({ value }) => {
const errors = [];
if (!value) {
errors.push({ message: 'Password is required via Custom.' });
}
return {
errors
};
};
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
};
<\/script>
`
}
};
},
methods: {
customPasswordResolver: ({ value }) => {
const errors = [];
if (!value) {
errors.push({ message: 'Password is required via Custom.' });
}
return {
errors
};
},
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>