127 lines
4.4 KiB
Vue
127 lines
4.4 KiB
Vue
<template>
|
|
<DocSectionText label="Built-in" :level="2" v-bind="$attrs">
|
|
<p>
|
|
Although PrimeVue components have built-in support for the Form API, you may still prefer to utilize the components as wrapped with the FormField. This is a matter of preference, for example in case you are also using FormField for other
|
|
3rd party components, your own custom components, and native elements, for consistency it may be an option.
|
|
</p>
|
|
</DocSectionText>
|
|
<div class="card flex justify-center">
|
|
<Form :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
<FormField v-slot="$field" name="username" initialValue="" class="flex flex-col gap-1">
|
|
<InputText type="text" placeholder="Username" />
|
|
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
|
|
</FormField>
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
</Form>
|
|
</div>
|
|
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
|
|
</template>
|
|
|
|
<script>
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
|
import { z } from 'zod';
|
|
|
|
export default {
|
|
data() {
|
|
return {
|
|
resolver: zodResolver(
|
|
z.object({
|
|
username: z.string().min(1, { message: 'Username is required.' })
|
|
})
|
|
),
|
|
code: {
|
|
basic: `
|
|
<Form :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
<FormField v-slot="$field" name="username" initialValue="" class="flex flex-col gap-1">
|
|
<InputText type="text" placeholder="Username" />
|
|
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
|
|
</FormField>
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
</Form>
|
|
`,
|
|
options: `
|
|
<template>
|
|
<div class="card flex justify-center">
|
|
<Toast />
|
|
|
|
<Form :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
<FormField v-slot="$field" name="username" initialValue="" class="flex flex-col gap-1">
|
|
<InputText type="text" placeholder="Username" />
|
|
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
|
|
</FormField>
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
</Form>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
|
import { z } from 'zod';
|
|
|
|
export default {
|
|
data() {
|
|
return {
|
|
resolver: zodResolver(
|
|
z.object({
|
|
username: z.string().min(1, { message: 'Username is required.' })
|
|
})
|
|
)
|
|
};
|
|
},
|
|
methods: {
|
|
onFormSubmit({ valid }) {
|
|
if (valid) {
|
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
}
|
|
}
|
|
}
|
|
};
|
|
<\/script>
|
|
`,
|
|
composition: `
|
|
<template>
|
|
<div class="card flex justify-center">
|
|
<Form :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
|
|
<FormField v-slot="$field" name="username" initialValue="" class="flex flex-col gap-1">
|
|
<InputText type="text" placeholder="Username" />
|
|
<Message v-if="$field?.invalid" severity="error" size="small" variant="simple">{{ $field.error?.message }}</Message>
|
|
</FormField>
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
</Form>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { reactive } from 'vue';
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
|
import { z } from 'zod';
|
|
import { useToast } from 'primevue/usetoast';
|
|
|
|
const toast = useToast();
|
|
|
|
const resolver = zodResolver(
|
|
z.object({
|
|
username: z.string().min(1, { message: 'Username is required.' })
|
|
})
|
|
);
|
|
|
|
const onFormSubmit = ({ valid }) => {
|
|
if (valid) {
|
|
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
}
|
|
};
|
|
<\/script>
|
|
`
|
|
}
|
|
};
|
|
},
|
|
methods: {
|
|
onFormSubmit({ valid }) {
|
|
if (valid) {
|
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
}
|
|
}
|
|
}
|
|
};
|
|
</script>
|