2024-10-23 04:05:27 +00:00
|
|
|
<template>
|
|
|
|
<DocSectionText v-bind="$attrs">
|
|
|
|
<p>
|
2024-10-31 07:56:10 +00:00
|
|
|
Validations are implemented the with <i>resolver</i> property. A custom resolver is responsible for handling the validation and returning an <i>errors</i> object with key-value pairs where key is the form field name and value is an array
|
|
|
|
of error object data. For productivity, we recommend using a schema validation library instead of building your own custom validation logic. The forms library provide built-in resolvers for popular options including
|
|
|
|
<a href="https://zod.dev/">Zod</a>, <a href="https://github.com/jquense/yup">Yup</a>, <a href="https://joi.dev/">Joi</a>, <a href="https://valibot.dev/">Valibot</a>, and <a href="https://docs.superstructjs.org/">Superstruct</a> that can
|
2024-11-01 21:54:30 +00:00
|
|
|
be be imported from <i>@primevue/forms/resolvers/*</i> path.
|
2024-10-23 04:05:27 +00:00
|
|
|
</p>
|
|
|
|
</DocSectionText>
|
|
|
|
<div class="card flex flex-col items-center gap-5">
|
|
|
|
<Fieldset legend="Schema">
|
|
|
|
<RadioButtonGroup v-model="selectedSchema" name="schema" class="flex flex-wrap gap-4" @update:modelValue="changeResolver">
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 04:05:27 +00:00
|
|
|
<RadioButton inputId="zod" value="Zod" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="zod">Zod</label>
|
2024-10-23 04:05:27 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 04:05:27 +00:00
|
|
|
<RadioButton inputId="yup" value="Yup" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="yup">Yup</label>
|
2024-10-23 04:05:27 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 04:05:27 +00:00
|
|
|
<RadioButton inputId="valibot" value="Valibot" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="valibot">Valibot</label>
|
2024-10-23 04:05:27 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 04:05:27 +00:00
|
|
|
<RadioButton inputId="superStruct" value="SuperStruct" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="superStruct">SuperStruct</label>
|
2024-10-23 04:05:27 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 04:05:27 +00:00
|
|
|
<RadioButton inputId="custom" value="Custom" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="custom">Custom</label>
|
2024-10-23 04:05:27 +00:00
|
|
|
</div>
|
|
|
|
</RadioButtonGroup>
|
|
|
|
</Fieldset>
|
|
|
|
|
2024-10-31 07:56:10 +00:00
|
|
|
<Form v-slot="$form" :initialValues :resolver="resolver" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
|
|
<div class="flex flex-col gap-1">
|
2024-10-23 04:05:27 +00:00
|
|
|
<InputText name="username" type="text" placeholder="Username" fluid />
|
2024-10-31 07:56:10 +00:00
|
|
|
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error.message }}</Message>
|
2024-10-23 04:05:27 +00:00
|
|
|
</div>
|
|
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
|
|
</Form>
|
|
|
|
</div>
|
2024-11-04 09:16:31 +00:00
|
|
|
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8', yup: '1.4.0', valibot: '0.42.1', superstruct: '2.0.2' }" />
|
2024-10-23 04:05:27 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2024-11-01 21:54:30 +00:00
|
|
|
import { superStructResolver } from '@primevue/forms/resolvers/superstruct';
|
|
|
|
import { valibotResolver } from '@primevue/forms/resolvers/valibot';
|
|
|
|
import { yupResolver } from '@primevue/forms/resolvers/yup';
|
|
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
2024-10-23 04:05:27 +00:00
|
|
|
import * as s from 'superstruct';
|
|
|
|
import * as v from 'valibot';
|
|
|
|
import * as yup from 'yup';
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
initialValues: {
|
|
|
|
username: ''
|
|
|
|
},
|
|
|
|
selectedSchema: 'Zod',
|
|
|
|
resolver: zodResolver(
|
|
|
|
z.object({
|
|
|
|
username: z.string().min(1, { message: 'Username is required via Zod.' })
|
|
|
|
})
|
|
|
|
),
|
|
|
|
code: {
|
|
|
|
basic: `
|
2024-10-31 07:56:10 +00:00
|
|
|
<Form v-slot="$form" :initialValues :resolver="resolver" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
|
|
<div class="flex flex-col gap-1">
|
2024-10-23 04:05:27 +00:00
|
|
|
<InputText name="username" type="text" placeholder="Username" fluid />
|
2024-10-31 07:56:10 +00:00
|
|
|
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error.message }}</Message>
|
2024-10-23 04:05:27 +00:00
|
|
|
</div>
|
|
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
|
|
</Form>
|
|
|
|
`,
|
|
|
|
options: `
|
|
|
|
<template>
|
2024-10-23 14:02:51 +00:00
|
|
|
<div class="card flex flex-col items-center gap-5">
|
|
|
|
<Toast />
|
|
|
|
|
|
|
|
<Fieldset legend="Schema">
|
|
|
|
<RadioButtonGroup v-model="selectedSchema" name="schema" class="flex flex-wrap gap-4" @update:modelValue="changeResolver">
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="zod" value="Zod" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="zod">Zod</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="yup" value="Yup" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="yup">Yup</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="valibot" value="Valibot" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="valibot">Valibot</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="superStruct" value="SuperStruct" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="superStruct">SuperStruct</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="custom" value="Custom" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="custom">Custom</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
|
|
|
</RadioButtonGroup>
|
|
|
|
</Fieldset>
|
|
|
|
|
2024-10-31 07:56:10 +00:00
|
|
|
<Form v-slot="$form" :initialValues :resolver="resolver" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
|
|
<div class="flex flex-col gap-1">
|
2024-10-23 14:02:51 +00:00
|
|
|
<InputText name="username" type="text" placeholder="Username" fluid />
|
2024-10-31 07:56:10 +00:00
|
|
|
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error.message }}</Message>
|
2024-10-23 04:05:27 +00:00
|
|
|
</div>
|
2024-10-23 14:02:51 +00:00
|
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
|
|
</Form>
|
|
|
|
</div>
|
2024-10-23 04:05:27 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2024-11-01 21:54:30 +00:00
|
|
|
import { superStructResolver } from '@primevue/forms/resolvers/superstruct';
|
|
|
|
import { valibotResolver } from '@primevue/forms/resolvers/valibot';
|
|
|
|
import { yupResolver } from '@primevue/forms/resolvers/yup';
|
|
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
2024-10-23 04:05:27 +00:00
|
|
|
import * as s from 'superstruct';
|
|
|
|
import * as v from 'valibot';
|
|
|
|
import * as yup from 'yup';
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
initialValues: {
|
|
|
|
username: ''
|
|
|
|
},
|
|
|
|
selectedSchema: 'Zod',
|
|
|
|
resolver: zodResolver(
|
|
|
|
z.object({
|
|
|
|
username: z.string().min(1, { message: 'Username is required via Zod.' })
|
|
|
|
})
|
|
|
|
)
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
changeResolver(schema) {
|
|
|
|
if (schema === 'Zod') {
|
|
|
|
this.resolver = zodResolver(
|
|
|
|
z.object({
|
|
|
|
username: z.string().min(1, { message: 'Username is required via Zod.' })
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Yup') {
|
|
|
|
this.resolver = yupResolver(
|
|
|
|
yup.object().shape({
|
|
|
|
username: yup.string().required('Username is required via Yup.')
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Valibot') {
|
|
|
|
this.resolver = valibotResolver(
|
|
|
|
v.object({
|
|
|
|
username: v.pipe(v.string(), v.minLength(1, 'Username is required via Valibot.'))
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'SuperStruct') {
|
|
|
|
this.resolver = superStructResolver(
|
|
|
|
s.object({
|
|
|
|
username: s.nonempty(s.string())
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Custom') {
|
|
|
|
this.resolver = ({ values }) => {
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if (!values.username) {
|
|
|
|
errors.username = [{ message: 'Username is required.' }];
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
errors
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onFormSubmit({ valid }) {
|
|
|
|
if (valid) {
|
|
|
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
<\/script>
|
|
|
|
`,
|
|
|
|
composition: `
|
|
|
|
<template>
|
2024-10-23 14:02:51 +00:00
|
|
|
<div class="card flex flex-col items-center gap-5">
|
|
|
|
<Toast />
|
|
|
|
|
|
|
|
<Fieldset legend="Schema">
|
|
|
|
<RadioButtonGroup v-model="selectedSchema" name="schema" class="flex flex-wrap gap-4" @update:modelValue="changeResolver">
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="zod" value="Zod" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="zod">Zod</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="yup" value="Yup" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="yup">Yup</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="valibot" value="Valibot" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="valibot">Valibot</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="superStruct" value="SuperStruct" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="superStruct">SuperStruct</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
2024-10-31 07:56:10 +00:00
|
|
|
<div class="flex items-center gap-2">
|
2024-10-23 14:02:51 +00:00
|
|
|
<RadioButton inputId="custom" value="Custom" />
|
2024-10-31 07:56:10 +00:00
|
|
|
<label for="custom">Custom</label>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
|
|
|
</RadioButtonGroup>
|
|
|
|
</Fieldset>
|
|
|
|
|
2024-10-31 07:56:10 +00:00
|
|
|
<Form v-slot="$form" :initialValues :resolver="resolver" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
|
|
<div class="flex flex-col gap-1">
|
2024-10-23 14:02:51 +00:00
|
|
|
<InputText name="username" type="text" placeholder="Username" fluid />
|
2024-10-31 07:56:10 +00:00
|
|
|
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error.message }}</Message>
|
2024-10-23 14:02:51 +00:00
|
|
|
</div>
|
|
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
|
|
</Form>
|
|
|
|
</div>
|
2024-10-23 04:05:27 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
2024-10-23 14:02:51 +00:00
|
|
|
import { ref, watch } from 'vue';
|
2024-11-01 21:54:30 +00:00
|
|
|
import { superStructResolver } from '@primevue/forms/resolvers/superstruct';
|
|
|
|
import { valibotResolver } from '@primevue/forms/resolvers/valibot';
|
|
|
|
import { yupResolver } from '@primevue/forms/resolvers/yup';
|
|
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
2024-10-23 14:02:51 +00:00
|
|
|
import * as s from 'superstruct';
|
|
|
|
import * as v from 'valibot';
|
|
|
|
import * as yup from 'yup';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { useToast } from 'primevue/usetoast';
|
|
|
|
|
|
|
|
const toast = useToast();
|
|
|
|
|
|
|
|
const initialValues = ref({
|
|
|
|
username: ''
|
|
|
|
});
|
|
|
|
const selectedSchema = ref('Zod');
|
|
|
|
|
|
|
|
const resolver = ref(zodResolver(
|
|
|
|
z.object({
|
|
|
|
username: z.string().min(1, { message: 'Username is required via Zod.' })
|
|
|
|
})
|
|
|
|
));
|
|
|
|
|
|
|
|
watch(selectedSchema, (newSchema) => {
|
|
|
|
changeResolver(newSchema);
|
|
|
|
});
|
|
|
|
|
2024-10-23 22:07:19 +00:00
|
|
|
const changeResolver = (schema) => {
|
2024-10-23 14:02:51 +00:00
|
|
|
if (schema === 'Zod') {
|
|
|
|
resolver.value = zodResolver(
|
|
|
|
z.object({
|
|
|
|
username: z.string().min(1, { message: 'Username is required via Zod.' })
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Yup') {
|
|
|
|
resolver.value = yupResolver(
|
|
|
|
yup.object().shape({
|
|
|
|
username: yup.string().required('Username is required via Yup.')
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Valibot') {
|
|
|
|
resolver.value = valibotResolver(
|
|
|
|
v.object({
|
|
|
|
username: v.pipe(v.string(), v.minLength(1, 'Username is required via Valibot.'))
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'SuperStruct') {
|
|
|
|
resolver.value = superStructResolver(
|
|
|
|
s.object({
|
|
|
|
username: s.nonempty(s.string())
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Custom') {
|
|
|
|
resolver.value = ({ values }) => {
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if (!values.username) {
|
|
|
|
errors.username = [{ message: 'Username is required.' }];
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
errors
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 22:07:19 +00:00
|
|
|
const onFormSubmit = ({ valid }) => {
|
|
|
|
if (valid) {
|
|
|
|
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
|
|
}
|
2024-10-23 14:02:51 +00:00
|
|
|
}
|
2024-10-23 04:05:27 +00:00
|
|
|
<\/script>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
changeResolver(schema) {
|
|
|
|
if (schema === 'Zod') {
|
|
|
|
this.resolver = zodResolver(
|
|
|
|
z.object({
|
|
|
|
username: z.string().min(1, { message: 'Username is required via Zod.' })
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Yup') {
|
|
|
|
this.resolver = yupResolver(
|
|
|
|
yup.object().shape({
|
|
|
|
username: yup.string().required('Username is required via Yup.')
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Valibot') {
|
|
|
|
this.resolver = valibotResolver(
|
|
|
|
v.object({
|
|
|
|
username: v.pipe(v.string(), v.minLength(1, 'Username is required via Valibot.'))
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'SuperStruct') {
|
|
|
|
this.resolver = superStructResolver(
|
|
|
|
s.object({
|
|
|
|
username: s.nonempty(s.string())
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (schema === 'Custom') {
|
|
|
|
this.resolver = ({ values }) => {
|
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if (!values.username) {
|
|
|
|
errors.username = [{ message: 'Username is required.' }];
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
errors
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onFormSubmit({ valid }) {
|
|
|
|
if (valid) {
|
|
|
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|