|
1 | 1 | <script setup lang="ts"> |
| 2 | +import { UButton, UCard, UContainer, UFormGroup, UIcon, UInput, UNotifications, useToast, UText } from 'unuse-ui' |
2 | 3 | import { ref } from 'vue' |
3 | 4 | import { useFormValidation } from 'vue-use-form-validation' |
4 | 5 | import * as z from 'zod' |
5 | 6 |
|
| 7 | +const toast = useToast() |
| 8 | +
|
6 | 9 | const schema = z.object({ |
7 | | - field1: z.string().min(1, 'field1 is required'), |
8 | | - field2: z.string().email('Invalid field2'), |
| 10 | + email: z.string().email(), |
| 11 | + password: z.string().min(8), |
9 | 12 | }) |
10 | 13 |
|
11 | 14 | const form = ref({ |
12 | | - field1: '', |
13 | | - field2: '', |
| 15 | + email: '', |
| 16 | + password: '', |
14 | 17 | }) |
15 | 18 |
|
16 | | -const { validate } = useFormValidation(schema, form) |
| 19 | +const { |
| 20 | + errors, |
| 21 | + errorCount, |
| 22 | + hasError, |
| 23 | + isLoading, |
| 24 | + isValid, |
| 25 | + focusFirstErroredInput, |
| 26 | + focusInput, |
| 27 | + getErrorMessage, |
| 28 | + validate, |
| 29 | +} = useFormValidation(schema, form) |
17 | 30 |
|
18 | | -await validate() |
| 31 | +async function onSubmit() { |
| 32 | + await validate() |
| 33 | + if (!isValid.value) { |
| 34 | + focusFirstErroredInput() |
| 35 | + return |
| 36 | + } |
| 37 | + toast.add({ title: 'Form is valid !', icon: 'icon-ph-check-circle', color: 'success' }) |
| 38 | +} |
19 | 39 | </script> |
20 | 40 |
|
21 | 41 | <template> |
22 | | - Hi |
| 42 | + <UContainer class="py-5"> |
| 43 | + <UText as="h1" label="Form example" class="mb-5" :ui="{ font: 'font-bold', size: 'text-2xl' }" /> |
| 44 | + <UCard v-if="hasError" class="mb-5" :ui="{ background: 'bg-red-200 dark:bg-red-600', body: { base: 'flex flex-col items-start gap-2' } }"> |
| 45 | + <UText :label="`Form has ${errorCount} ${errorCount > 1 ? 'errors' : 'error'}:`" :ui="{ font: 'font-bold', size: 'text-lg' }" class="mb-1" /> |
| 46 | + <div |
| 47 | + v-for=" |
| 48 | + error, i in Object.keys(errors) as Array<keyof typeof errors> |
| 49 | + " |
| 50 | + :key="i" |
| 51 | + class="flex items-center" |
| 52 | + > |
| 53 | + <UIcon name="icon-ph-dot-bold" color="dark" /> |
| 54 | + <UButton |
| 55 | + class="ml-3" color="dark" :is-padded="false" variant="link" |
| 56 | + :label="getErrorMessage(error)" |
| 57 | + @click="focusInput({ inputName: error })" |
| 58 | + /> |
| 59 | + </div> |
| 60 | + </UCard> |
| 61 | + <form class="flex flex-col gap-3"> |
| 62 | + <UFormGroup label="Email" :error="getErrorMessage('email')" is-required> |
| 63 | + < UInput v-model= "form.email" name= "email" type= "email" placeholder= "[email protected]" autofocus size= "md" /> |
| 64 | + </UFormGroup> |
| 65 | + <UFormGroup label="Password" :error="getErrorMessage('password')" is-required> |
| 66 | + <UInput v-model="form.password" name="password" type="password" placeholder="**********" size="md" /> |
| 67 | + </UFormGroup> |
| 68 | + <UButton label="Submit" is-block :is-loading="isLoading" @click="onSubmit" /> |
| 69 | + </form> |
| 70 | + </UContainer> |
| 71 | + <Teleport to="body"> |
| 72 | + <UNotifications /> |
| 73 | + </Teleport> |
23 | 74 | </template> |
0 commit comments