primevue-mirror/apps/showcase/doc/forms/formfield/BuiltInDoc.vue

127 lines
4.4 KiB
Vue
Raw Normal View History

2024-11-01 06:00:00 +00:00
<template>
<DocSectionText label="Built-in" :level="2" v-bind="$attrs">
2024-11-01 08:22:44 +00:00
<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>
2024-11-01 06:00:00 +00:00
</DocSectionText>
<div class="card flex justify-center">
<Form :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="username" initialValue="" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<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>
2024-11-01 21:54:30 +00:00
import { zodResolver } from '@primevue/forms/resolvers/zod';
2024-11-01 06:00:00 +00:00
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">
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="username" initialValue="" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<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">
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="username" initialValue="" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<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>
2024-11-01 21:54:30 +00:00
import { zodResolver } from '@primevue/forms/resolvers/zod';
2024-11-01 06:00:00 +00:00
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">
2024-11-01 08:22:44 +00:00
<FormField v-slot="$field" name="username" initialValue="" class="flex flex-col gap-1">
2024-11-01 06:00:00 +00:00
<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';
2024-11-01 21:54:30 +00:00
import { zodResolver } from '@primevue/forms/resolvers/zod';
2024-11-01 06:00:00 +00:00
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>