InputText updates

pull/6632/head
tugcekucukoglu 2024-10-23 11:16:56 +03:00
parent 8c68ce77eb
commit 58c05c646d
1 changed files with 70 additions and 14 deletions

View File

@ -15,7 +15,7 @@
<Button type="submit" severity="secondary" label="Submit" /> <Button type="submit" severity="secondary" label="Submit" />
</Form> </Form>
</div> </div>
<DocSectionCode :code="code" /> <DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
</template> </template>
<script> <script>
@ -29,11 +29,12 @@ export default {
username: '', username: '',
email: '' email: ''
}, },
resolver: null, resolver: zodResolver(
schema: z.object({ z.object({
username: z.string().min(1, { message: 'Username is required.' }), username: z.string().min(1, { message: 'Username is required.' }),
email: z.string().min(1, { message: 'Email is required.' }).email({ message: 'Email must be valid.' }) email: z.string().min(1, { message: 'Email is required.' }).email({ message: 'Email must be valid.' })
}), })
),
code: { code: {
basic: ` basic: `
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4"> <Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
@ -45,21 +46,50 @@ export default {
<InputText name="email" type="text" placeholder="Email" /> <InputText name="email" type="text" placeholder="Email" />
<Message v-if="$form.email?.invalid" severity="error">{{ $form.email.errors[0]?.message }}</Message> <Message v-if="$form.email?.invalid" severity="error">{{ $form.email.errors[0]?.message }}</Message>
</div> </div>
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button> <Button type="submit" severity="secondary" label="Submit" />
</Form> </Form>
`, `,
options: ` options: `
<template> <template>
<div class="card flex justify-center"> <div class="card flex justify-center">
<InputText type="text" v-model="value" /> <Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @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.errors[0]?.message }}</Message>
</div>
<div class="flex flex-col gap-2">
<InputText name="email" type="text" placeholder="Email" fluid />
<Message v-if="$form.email?.invalid" severity="error">{{ $form.email.errors[0]?.message }}</Message>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div> </div>
</template> </template>
<script> <script>
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
export default { export default {
data() { data() {
return { return {
value: null initialValues: {
username: '',
email: ''
},
resolver: zodResolver(
z.object({
username: z.string().min(1, { message: 'Username is required.' }),
email: z.string().min(1, { message: 'Email is required.' }).email({ message: 'Email must be valid.' })
})
)
}
},
methods: {
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
} }
} }
} }
@ -69,22 +99,48 @@ export default {
composition: ` composition: `
<template> <template>
<div class="card flex justify-center"> <div class="card flex justify-center">
<InputText type="text" v-model="value" /> <Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @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.errors[0]?.message }}</Message>
</div>
<div class="flex flex-col gap-2">
<InputText name="email" type="text" placeholder="Email" fluid />
<Message v-if="$form.email?.invalid" severity="error">{{ $form.email.errors[0]?.message }}</Message>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div> </div>
</template> </template>
<script setup> <script setup>
import { ref } from 'vue'; import { ref } from 'vue';
import { zodResolver } from '@primevue/form/resolvers';
import { useToast } from "primevue/usetoast";
import { z } from 'zod';
const value = ref(null); const toast = useToast();
const initialValues = ref({
username: '',
email: ''
});
const resolver = ref(zodResolver(
z.object({
username: z.string().min(1, { message: 'Username is required.' }),
email: z.string().min(1, { message: 'Email is required.' }).email({ message: 'Email must be valid.' })
})
));
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
};
<\/script> <\/script>
` `
} }
}; };
}, },
mounted() {
this.resolver = zodResolver(this.schema);
},
methods: { methods: {
onFormSubmit({ valid }) { onFormSubmit({ valid }) {
if (valid) { if (valid) {