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

234 lines
9.9 KiB
Vue
Raw Normal View History

2024-10-23 04:05:27 +00:00
<template>
<DocSectionText v-bind="$attrs">
2024-10-31 07:56:10 +00:00
<p>
The <i>submit</i> callback returns an object that encapsulates the form's validity, any existing errors, and its current state. This enables access to the form values, validation status, and any errors present at the time of submission.
View the <i>FormSubmitEvent</i> in the API documentation for more information about the available event data.
</p>
2024-10-23 04:05:27 +00:00
</DocSectionText>
<div class="card flex justify-center">
2024-10-23 21:57:52 +00:00
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-60">
2024-10-31 07:56:10 +00:00
<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>
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
2024-10-23 21:57:52 +00:00
<Password name="password" placeholder="Password" :feedback="false" toggleMask fluid />
2024-10-31 07:56:10 +00:00
<Message v-if="$form.password?.invalid" severity="error" size="small" variant="simple">
<ul class="my-0 px-4 flex flex-col gap-1">
<li v-for="(error, index) of $form.password.errors" :key="index">{{ error.message }}</li>
2024-10-23 04:05:27 +00:00
</ul>
</Message>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
2024-10-23 11:06:59 +00:00
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
2024-10-23 04:05:27 +00:00
</template>
<script>
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
export default {
data() {
return {
initialValues: {
username: '',
password: ''
},
resolver: zodResolver(
z.object({
2024-10-23 04:11:19 +00:00
username: z.string().min(1, { message: 'Username is required.' }),
2024-10-23 04:05:27 +00:00
password: z
.string()
2024-10-23 12:22:53 +00:00
.min(3, { message: 'Minimum 3 characters.' })
.max(8, { message: 'Maximum 8 characters.' })
2024-10-23 04:05:27 +00:00
.refine((value) => /[a-z]/.test(value), {
2024-10-23 12:22:53 +00:00
message: 'Must have a lowercase letter.'
2024-10-23 04:05:27 +00:00
})
.refine((value) => /[A-Z]/.test(value), {
2024-10-23 12:22:53 +00:00
message: 'Must have a uppercase letter.'
2024-10-23 04:05:27 +00:00
})
.refine((value) => /\d/.test(value), {
2024-10-23 12:22:53 +00:00
message: 'Must have a number.'
2024-10-23 04:05:27 +00:00
})
})
),
code: {
basic: `
2024-10-23 21:57:52 +00:00
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-60">
2024-10-31 07:56:43 +00:00
<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:43 +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-31 07:56:43 +00:00
<div class="flex flex-col gap-1">
2024-10-23 21:57:52 +00:00
<Password name="password" placeholder="Password" :feedback="false" toggleMask fluid />
2024-10-31 07:56:43 +00:00
<Message v-if="$form.password?.invalid" severity="error" size="small" variant="simple">
<ul class="my-0 px-4 flex flex-col gap-1">
<li v-for="(error, index) of $form.password.errors" :key="index">{{ error.message }}</li>
2024-10-23 04:05:27 +00:00
</ul>
</Message>
</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-23 21:57:52 +00:00
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-60">
2024-10-31 07:56:43 +00:00
<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:43 +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:43 +00:00
<div class="flex flex-col gap-1">
2024-10-23 21:57:52 +00:00
<Password name="password" placeholder="Password" :feedback="false" toggleMask fluid />
2024-10-31 07:56:43 +00:00
<Message v-if="$form.password?.invalid" severity="error" size="small" variant="simple">
<ul class="my-0 px-4 flex flex-col gap-1">
<li v-for="(error, index) of $form.password.errors" :key="index">{{ error.message }}</li>
2024-10-23 14:02:51 +00:00
</ul>
</Message>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
2024-10-23 04:05:27 +00:00
</template>
<script>
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
export default {
data() {
return {
initialValues: {
username: '',
password: ''
},
resolver: zodResolver(
z.object({
2024-10-23 04:11:19 +00:00
username: z.string().min(1, { message: 'Username is required.' }),
2024-10-23 04:05:27 +00:00
password: z
.string()
2024-10-23 12:22:53 +00:00
.min(3, { message: 'Minimum 3 characters.' })
.max(8, { message: 'Maximum 8 characters.' })
2024-10-23 04:05:27 +00:00
.refine((value) => /[a-z]/.test(value), {
2024-10-23 12:22:53 +00:00
message: 'Must have a lowercase letter.'
2024-10-23 04:05:27 +00:00
})
.refine((value) => /[A-Z]/.test(value), {
2024-10-23 12:22:53 +00:00
message: 'Must have a uppercase letter.'
2024-10-23 04:05:27 +00:00
})
.refine((value) => /\d/.test(value), {
2024-10-23 12:22:53 +00:00
message: 'Must have a number.'
2024-10-23 04:05:27 +00:00
})
})
)
};
},
methods: {
onFormSubmit(e) {
// e.originalEvent: Represents the native form submit event.
// e.valid: A boolean that indicates whether the form is valid or not.
// e.states: Contains the current state of each form field, including validity status.
// e.errors: An object that holds any validation errors for the invalid fields in the form.
// e.values: An object containing the current values of all form fields.
2024-10-23 14:02:51 +00:00
// e.reset: A function that resets the form to its initial state.
2024-10-23 04:05:27 +00:00
if (e.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-23 21:57:52 +00:00
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-60">
2024-10-31 07:56:43 +00:00
<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:43 +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:43 +00:00
<div class="flex flex-col gap-1">
2024-10-23 21:57:52 +00:00
<Password name="password" placeholder="Password" :feedback="false" toggleMask fluid />
2024-10-31 07:56:43 +00:00
<Message v-if="$form.password?.invalid" severity="error" size="small" variant="simple">
<ul class="my-0 px-4 flex flex-col gap-1">
<li v-for="(error, index) of $form.password.errors" :key="index">{{ error.message }}</li>
2024-10-23 14:02:51 +00:00
</ul>
</Message>
</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 } from 'vue';
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
import { useToast } from 'primevue/usetoast';
const toast = useToast();
const initialValues = ref({
username: '',
password: ''
});
const resolver = zodResolver(
z.object({
username: z.string().min(1, { message: 'Username is required.' }),
password: z
.string()
.min(3, { message: 'Minimum 3 characters.' })
.max(8, { message: 'Maximum 8 characters.' })
.refine((value) => /[a-z]/.test(value), {
message: 'Must have a lowercase letter.'
})
.refine((value) => /[A-Z]/.test(value), {
message: 'Must have an uppercase letter.'
})
.refine((value) => /\d/.test(value), {
message: 'Must have a number.'
})
})
);
const onFormSubmit = (e) => {
// e.originalEvent: Represents the native form submit event.
// e.valid: A boolean that indicates whether the form is valid or not.
// e.states: Contains the current state of each form field, including validity status.
// e.errors: An object that holds any validation errors for the invalid fields in the form.
// e.values: An object containing the current values of all form fields.
// e.reset: A function that resets the form to its initial state.
if (e.valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
};
2024-10-23 04:05:27 +00:00
<\/script>
`
}
};
},
methods: {
onFormSubmit(e) {
// e.originalEvent: Represents the native form submit event.
// e.valid: A boolean that indicates whether the form is valid or not.
// e.states: Contains the current state of each form field, including validity status.
// e.errors: An object that holds any validation errors for the invalid fields in the form.
// e.values: An object containing the current values of all form fields.
2024-10-23 10:44:10 +00:00
// e.reset: A function that resets the form to its initial state.
2024-10-23 14:02:51 +00:00
2024-10-23 04:05:27 +00:00
if (e.valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>