181 lines
5.6 KiB
Vue
181 lines
5.6 KiB
Vue
<template>
|
|
<DocSectionText v-bind="$attrs">
|
|
<p>
|
|
All PrimeVue form components are designed for seamless integration with the forms library. Instead of using the standard <i>v-model</i>, the <i>name</i> property is used to link a state object that tracks values, errors, and actions. The
|
|
form component provides four key properties for state management.
|
|
</p>
|
|
</DocSectionText>
|
|
<div class="doc-tablewrapper">
|
|
<table class="doc-table">
|
|
<thead>
|
|
<tr>
|
|
<th class="w-1/4">Property</th>
|
|
<th class="w-3/4">Description</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>v-slot="$form"</td>
|
|
<td>Exposes the main <i>$form</i> object that tracks the state management of the fields.</td>
|
|
</tr>
|
|
<tr>
|
|
<td>initialValues</td>
|
|
<td>Specifies the default values to initiate the form with.</td>
|
|
</tr>
|
|
<tr>
|
|
<td>resolver</td>
|
|
<td>The validation handler to implement validations or to bind a schema like <i>Zod</i>, <i>Yup</i>, <i>Valibot</i> and more.</td>
|
|
</tr>
|
|
<tr>
|
|
<td>@submit</td>
|
|
<td>The event handler to execute when the form is submitted.</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="card flex justify-center">
|
|
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
<div class="flex flex-col gap-1">
|
|
<InputText name="username" type="text" placeholder="Username" fluid />
|
|
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error?.message }}</Message>
|
|
</div>
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
</Form>
|
|
</div>
|
|
<DocSectionCode :code="code" />
|
|
</template>
|
|
|
|
<script>
|
|
export default {
|
|
data() {
|
|
return {
|
|
initialValues: {
|
|
username: ''
|
|
},
|
|
code: {
|
|
basic: `
|
|
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
<div class="flex flex-col gap-1">
|
|
<InputText name="username" type="text" placeholder="Username" fluid />
|
|
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error?.message }}</Message>
|
|
</div>
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
</Form>
|
|
`,
|
|
options: `
|
|
<template>
|
|
<div class="card flex justify-center">
|
|
<Toast />
|
|
|
|
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
<div class="flex flex-col gap-1">
|
|
<InputText name="username" type="text" placeholder="Username" fluid />
|
|
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error?.message }}</Message>
|
|
</div>
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
</Form>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
export default {
|
|
data() {
|
|
return {
|
|
initialValues: {
|
|
username: ''
|
|
}
|
|
};
|
|
},
|
|
methods: {
|
|
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>
|
|
<div class="card flex justify-center">
|
|
<Toast />
|
|
|
|
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
<div class="flex flex-col gap-1">
|
|
<InputText name="username" type="text" placeholder="Username" fluid />
|
|
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error?.message }}</Message>
|
|
</div>
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
</Form>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { reactive } from 'vue';
|
|
import { useToast } from 'primevue/usetoast';
|
|
|
|
const toast = useToast();
|
|
|
|
const initialValues = reactive({
|
|
username: ''
|
|
});
|
|
|
|
const resolver = ({ values }) => {
|
|
const errors = {};
|
|
|
|
if (!values.username) {
|
|
errors.username = [{ message: 'Username is required.' }];
|
|
}
|
|
|
|
return {
|
|
errors
|
|
};
|
|
};
|
|
|
|
const onFormSubmit = ({ valid }) => {
|
|
if (valid) {
|
|
toast.add({
|
|
severity: 'success',
|
|
summary: 'Form is submitted.',
|
|
life: 3000
|
|
});
|
|
}
|
|
};
|
|
<\/script>
|
|
`
|
|
}
|
|
};
|
|
},
|
|
methods: {
|
|
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>
|