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

211 lines
7.7 KiB
Vue
Raw Normal View History

2024-10-23 10:38:00 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>
Form component supports flexible validation triggers, allowing validation on value updates, blur events, form mount, or submission, with options to apply this behavior globally or to specific fields via <i>validateOnValueUpdate</i>,
2024-10-23 21:19:13 +00:00
<i>validateOnBlur</i>, <i>validateOnMount</i>, and <i>validateOnSubmit</i>, which can also be set in PrimeVue components using <i>formControl</i> property.
2024-10-23 10:38:00 +00:00
</p>
</DocSectionText>
<div class="card flex justify-center">
<Form v-slot="$form" :initialValues :resolver :validateOnValueUpdate="false" :validateOnBlur="true" :validateOnMount="['name']" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
<div class="flex flex-col gap-2">
<InputText name="username" type="text" placeholder="Username" fluid />
2024-10-23 11:59:52 +00:00
<Message v-if="$form.username?.invalid" severity="error">{{ $form.username.error.message }}</Message>
2024-10-23 10:38:00 +00:00
</div>
<div class="flex flex-col gap-2">
2024-10-23 21:19:13 +00:00
<InputText name="name" type="text" placeholder="Name" fluid :formControl="{ validateOnValueUpdate: true }" />
2024-10-23 11:59:52 +00:00
<Message v-if="$form.name?.invalid" severity="error">{{ $form.name.error.message }}</Message>
2024-10-23 10:38:00 +00:00
</div>
<div class="flex flex-col gap-2">
<InputText name="surname" type="text" placeholder="Surname" fluid />
2024-10-23 11:59:52 +00:00
<Message v-if="$form.surname?.invalid" severity="error">{{ $form.surname.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: '',
name: '',
surname: ''
},
code: {
basic: `
<Form v-slot="$form" :initialValues :resolver :validateOnValueUpdate="false" :validateOnBlur="true" :validateOnMount="['name']" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
<div class="flex flex-col gap-2">
<InputText name="username" type="text" placeholder="Username" fluid />
2024-10-23 11:59:52 +00:00
<Message v-if="$form.username?.invalid" severity="error">{{ $form.username.error.message }}</Message>
2024-10-23 10:38:00 +00:00
</div>
<div class="flex flex-col gap-2">
2024-10-23 21:19:13 +00:00
<InputText name="name" type="text" placeholder="Name" fluid :formControl="{ validateOnValueUpdate: true }" />
2024-10-23 11:59:52 +00:00
<Message v-if="$form.name?.invalid" severity="error">{{ $form.name.error.message }}</Message>
2024-10-23 10:38:00 +00:00
</div>
<div class="flex flex-col gap-2">
<InputText name="surname" type="text" placeholder="Surname" fluid />
2024-10-23 11:59:52 +00:00
<Message v-if="$form.surname?.invalid" severity="error">{{ $form.surname.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 />
<Form v-slot="$form" :initialValues :resolver :validateOnValueUpdate="false" :validateOnBlur="true" :validateOnMount="['name']" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
<div class="flex flex-col gap-2">
<InputText name="username" type="text" placeholder="Username" fluid />
<Message v-if="$form.username?.invalid" severity="error">{{ $form.username.error.message }}</Message>
</div>
<div class="flex flex-col gap-2">
2024-10-23 21:19:13 +00:00
<InputText name="name" type="text" placeholder="Name" fluid :formControl="{ validateOnValueUpdate: true }"/>
2024-10-23 14:02:51 +00:00
<Message v-if="$form.name?.invalid" severity="error">{{ $form.name.error.message }}</Message>
</div>
<div class="flex flex-col gap-2">
<InputText name="surname" type="text" placeholder="Surname" fluid />
<Message v-if="$form.surname?.invalid" severity="error">{{ $form.surname.error.message }}</Message>
</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: '',
name: '',
surname: ''
2024-10-23 10:38:00 +00:00
}
};
},
methods: {
resolver: ({ values }) => {
const errors = {};
if (!values.username) {
errors.username = [{ message: 'Username is required.' }];
}
2024-10-23 14:02:51 +00:00
if (!values.name) {
errors.name = [{ message: 'Name is required.' }];
}
if (!values.surname) {
errors.surname = [{ message: 'Surname 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>
`,
composition: `
<template>
2024-10-23 14:02:51 +00:00
<div class="card flex justify-center">
<Toast />
<Form v-slot="$form" :initialValues :resolver :validateOnValueUpdate="false" :validateOnBlur="true" :validateOnMount="['name']" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
<div class="flex flex-col gap-2">
<InputText name="username" type="text" placeholder="Username" fluid />
<Message v-if="$form.username?.invalid" severity="error">{{ $form.username.error.message }}</Message>
</div>
<div class="flex flex-col gap-2">
2024-10-23 21:19:13 +00:00
<InputText name="name" type="text" placeholder="Name" fluid :formControl="{ validateOnValueUpdate: true }" />
2024-10-23 14:02:51 +00:00
<Message v-if="$form.name?.invalid" severity="error">{{ $form.name.error.message }}</Message>
</div>
<div class="flex flex-col gap-2">
<InputText name="surname" type="text" placeholder="Surname" fluid />
<Message v-if="$form.surname?.invalid" severity="error">{{ $form.surname.error.message }}</Message>
</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: '',
name: '',
surname: ''
});
const resolver = ({ values }) => {
const errors = {};
if (!values.username) {
errors.username = [{ message: 'Username is required.' }];
}
if (!values.name) {
errors.name = [{ message: 'Name is required.' }];
}
if (!values.surname) {
errors.surname = [{ message: 'Surname is required.' }];
}
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.' }];
}
if (!values.name) {
errors.name = [{ message: 'Name is required.' }];
}
if (!values.surname) {
errors.surname = [{ message: 'Surname is required.' }];
}
return {
errors
};
},
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>