primevue-mirror/apps/showcase/doc/forms/ValidateOnDoc.vue

212 lines
8.5 KiB
Vue
Raw Normal View History

2024-10-23 10:38:00 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>
2024-10-31 07:56:10 +00:00
Form component supports flexible validation triggers, allowing validation on value updates, blur events, form mount, or submission. These behaviors can be configured at form level or on specific fields via the
<i>validateOnValueUpdate</i>, <i>validateOnBlur</i>, <i>validateOnMount</i>, and <i>validateOnSubmit</i> options of the <i>formControl</i> property.
2024-10-23 10:38:00 +00:00
</p>
2024-10-31 07:56:10 +00:00
<p>In this example, form disables <i>validateOnValueUpdate</i> and enables <i>validateOnBlur</i> at form level, and validates <b>firstName</b> on mount. The <i>firstName</i> field, overrides the form level setting locally.</p>
2024-10-23 10:38:00 +00:00
</DocSectionText>
<div class="card flex justify-center">
2024-10-31 07:56:10 +00:00
<Form v-slot="$form" :initialValues :resolver :validateOnValueUpdate="false" :validateOnBlur="true" :validateOnMount="['firstName']" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
<div class="flex flex-col gap-1">
2024-10-23 10:38:00 +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 10:38:00 +00:00
</div>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
<InputText name="firstName" type="text" placeholder="First Name" fluid :formControl="{ validateOnValueUpdate: true }" />
<Message v-if="$form.firstName?.invalid" severity="error" size="small" variant="simple">{{ $form.firstName.error.message }}</Message>
2024-10-23 10:38:00 +00:00
</div>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
<InputText name="lastName" type="text" placeholder="Last Name" fluid />
<Message v-if="$form.lastName?.invalid" severity="error" size="small" variant="simple">{{ $form.lastName.error.message }}</Message>
2024-10-23 10:38:00 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
<DocSectionCode :code="code" />
</template>
<script>
export default {
data() {
return {
initialValues: {
username: '',
2024-10-31 07:56:10 +00:00
firstName: '',
lastName: ''
2024-10-23 10:38:00 +00:00
},
code: {
basic: `
2024-10-31 07:56:10 +00:00
<Form v-slot="$form" :initialValues :resolver :validateOnValueUpdate="false" :validateOnBlur="true" :validateOnMount="['firstName']" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
<div class="flex flex-col gap-1">
2024-10-23 10:38:00 +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 10:38:00 +00:00
</div>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
<InputText name="firstName" type="text" placeholder="First Name" fluid :formControl="{ validateOnValueUpdate: true }" />
<Message v-if="$form.firstName?.invalid" severity="error" size="small" variant="simple">{{ $form.firstName.error.message }}</Message>
2024-10-23 10:38:00 +00:00
</div>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
<InputText name="lastName" type="text" placeholder="Last Name" fluid />
<Message v-if="$form.lastName?.invalid" severity="error" size="small" variant="simple">{{ $form.lastName.error.message }}</Message>
2024-10-23 10:38:00 +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 justify-center">
<Toast />
2024-10-31 07:56:10 +00:00
<Form v-slot="$form" :initialValues :resolver :validateOnValueUpdate="false" :validateOnBlur="true" :validateOnMount="['firstName']" @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>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
<InputText name="firstName" type="text" placeholder="First Name" fluid :formControl="{ validateOnValueUpdate: true }" />
<Message v-if="$form.firstName?.invalid" severity="error" size="small" variant="simple">{{ $form.firstName.error.message }}</Message>
2024-10-23 14:02:51 +00:00
</div>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
<InputText name="lastName" type="text" placeholder="Last Name" fluid />
<Message v-if="$form.lastName?.invalid" severity="error" size="small" variant="simple">{{ $form.lastName.error.message }}</Message>
2024-10-23 14:02:51 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
2024-10-23 10:38:00 +00:00
</template>
<script>
export default {
data() {
return {
initialValues: {
2024-10-23 14:02:51 +00:00
username: '',
2024-10-31 07:56:10 +00:00
firstName: '',
lastName: ''
2024-10-23 10:38:00 +00:00
}
};
},
methods: {
resolver: ({ values }) => {
const errors = {};
if (!values.username) {
errors.username = [{ message: 'Username is required.' }];
}
2024-10-31 07:56:10 +00:00
if (!values.firstName) {
errors.firstName = [{ message: 'First name is required.' }];
2024-10-23 14:02:51 +00:00
}
2024-10-31 07:56:10 +00:00
if (!values.lastName) {
errors.lastName = [{ message: 'Last name is required.' }];
2024-10-23 14:02:51 +00:00
}
2024-10-23 10:38:00 +00:00
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 justify-center">
<Toast />
2024-10-31 07:56:10 +00:00
<Form v-slot="$form" :initialValues :resolver :validateOnValueUpdate="false" :validateOnBlur="true" :validateOnMount="['firstName']" @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>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
<InputText name="firstName" type="text" placeholder="First Name" fluid :formControl="{ validateOnValueUpdate: true }" />
<Message v-if="$form.firstName?.invalid" severity="error" size="small" variant="simple">{{ $form.firstName.error.message }}</Message>
2024-10-23 14:02:51 +00:00
</div>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
<InputText name="lastName" type="text" placeholder="Last Name" fluid />
<Message v-if="$form.lastName?.invalid" severity="error" size="small" variant="simple">{{ $form.lastName.error.message }}</Message>
2024-10-23 14:02:51 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
2024-10-23 10:38:00 +00:00
</template>
<script setup>
2024-10-23 14:02:51 +00:00
import { ref } from 'vue';
import { useToast } from 'primevue/usetoast';
const toast = useToast();
const initialValues = ref({
username: '',
2024-10-31 07:56:10 +00:00
firstName: '',
lastName: ''
2024-10-23 14:02:51 +00:00
});
const resolver = ({ values }) => {
const errors = {};
if (!values.username) {
errors.username = [{ message: 'Username is required.' }];
}
if (!values.name) {
2024-10-31 07:56:10 +00:00
errors.firstName = [{ message: 'First name is required.' }];
2024-10-23 14:02:51 +00:00
}
if (!values.surname) {
2024-10-31 07:56:10 +00:00
errors.lastName = [{ message: 'Last name is required.' }];
2024-10-23 14:02:51 +00:00
}
return {
errors
};
};
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
2024-10-23 10:38:00 +00:00
<\/script>
`
}
};
},
methods: {
resolver: ({ values }) => {
const errors = {};
if (!values.username) {
errors.username = [{ message: 'Username is required.' }];
}
2024-10-31 07:56:10 +00:00
if (!values.firstName) {
errors.firstName = [{ message: 'First name is required.' }];
2024-10-23 10:38:00 +00:00
}
2024-10-31 07:56:10 +00:00
if (!values.lastName) {
errors.lastName = [{ message: 'Last name is required.' }];
2024-10-23 10:38:00 +00:00
}
return {
errors
};
},
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>