Skip to content

Commit

Permalink
[Page] [Form] Add with validate form
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Mar 25, 2024
1 parent d3d04a4 commit be20067
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/data/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const createNavigation = (): void => {
const formWithSplitField = createNavigationItem('form.common.formWithSplitField', undefined, '/forms/withSplitField', AudioWaveform, NavigationPosition.LEFT_TOP)
const formWithImage = createNavigationItem('form.common.formWithImage', undefined, '/forms/withImage', AudioWaveform, NavigationPosition.LEFT_TOP)
const formWithThird = createNavigationItem('form.common.formWithThird', undefined, '/forms/withThird', AudioWaveform, NavigationPosition.LEFT_TOP)
const formArray = [form, formWithAction, formWithSplitField, formWithImage, formWithThird]
const formWithValidate = createNavigationItem('form.common.formWithValidate', undefined, '/forms/withValidate', AudioWaveform, NavigationPosition.LEFT_TOP)
const formArray = [form, formWithAction, formWithSplitField, formWithImage, formWithThird, formWithValidate]
const froms = createNavigationItem('form.common.form', formArray.length.toString(), '/forms', FormInput, NavigationPosition.LEFT_TOP,
formArray, undefined, 'common.common.form')
NavigationService.addNavigation(froms)
Expand Down
6 changes: 4 additions & 2 deletions src/i18n/langs/en/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default {
firstName: 'First Name',
lastName: 'Last Name',
formWithImage: 'Form with Image',
formWithThird: 'Forms with third-party logins'
formWithThird: 'Forms with third-party logins',
formWithValidate: 'Form with validation'
},
tip: {
formBasic: 'This is a basic form example',
Expand All @@ -18,6 +19,7 @@ export default {
firstNameHolder: 'Please enter first name',
lastNameHolder: 'Please enter last name',
formWithImage: 'Here\'s an example of a form with an image',
formWithThird: 'Here\'s an example of a form with a third-party login'
formWithThird: 'Here\'s an example of a form with a third-party login',
formWithValidate: 'Here\'s an example of a form with validation'
}
}
6 changes: 4 additions & 2 deletions src/i18n/langs/zhCn/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default {
firstName: '名',
lastName: '姓',
formWithImage: '带图片的表单',
formWithThird: '带第三方登录的表单'
formWithThird: '带第三方登录的表单',
formWithValidate: '带校验的表单'
},
tip: {
formBasic: '这是一个表单的基础示例',
Expand All @@ -18,6 +19,7 @@ export default {
firstNameHolder: '请输入名',
lastNameHolder: '请输入姓',
formWithImage: '这是一个带有图片的表单示例',
formWithThird: '这是一个带有第三方登录的表单示例'
formWithThird: '这是一个带有第三方登录的表单示例',
formWithValidate: '这是一个带有校验的表单示例'
}
}
5 changes: 5 additions & 0 deletions src/router/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ const createFormRouter = (router: Router): void => {
name: 'withThird',
path: 'withThird',
component: () => import('@/views/pages/form/FormWithThird.vue')
},
{
name: 'withValidate',
path: 'withValidate',
component: () => import('@/views/pages/form/FormWithValidate.vue')
}
]
})
Expand Down
110 changes: 110 additions & 0 deletions src/views/pages/form/FormWithValidate.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<template>
<div class="container grid h-svh flex-col items-center justify-center bg-primary-foreground lg:max-w-none lg:px-0">
<div class="mx-auto flex w-full flex-col justify-center space-y-2 sm:w-[480px] lg:p-8">
<Card>
<CardHeader>
<CardTitle class="text-2xl">{{ $t('form.common.formWithValidate') }}</CardTitle>
<CardDescription>{{ $t('form.tip.formWithValidate') }}</CardDescription>
</CardHeader>
<CardContent>
<form @submit="onSubmit">
<div class="grid gap-2">
<FormField v-slot="{ componentField }" name="username">
<FormItem>
<FormLabel>{{ $t('user.common.username') }}</FormLabel>
<FormControl>
<Input type="text" v-model="formState.username" v-bind="componentField" :placeholder="$t('user.tip.usernameHolder')"/>
</FormControl>
<FormMessage/>
</FormItem>
</FormField>
</div>
<div class="grid gap-2 mt-1">
<FormField v-slot="{ componentField }" name="password">
<FormItem>
<div class="flex items-center justify-between">
<FormLabel>{{ $t('user.common.password') }}</FormLabel>
<RouterLink to="/user/forgot/password" class="text-sm font-medium text-muted-foreground hover:opacity-75">
{{ $t('common.common.forgotPassword') }}?
</RouterLink>
</div>
<FormControl>
<Input v-model="formState.password" v-bind="componentField" type="password" :placeholder="$t('user.tip.passwordHolder')"/>
</FormControl>
<FormMessage/>
</FormItem>
</FormField>
</div>
<Button class="mt-3 w-full" type="submit" :disabled="loading">
<Loader2 v-if="loading" class="mr-2 h-4 w-4 animate-spin"/>
{{ $t('common.common.signIn') }}
</Button>
</form>
</CardContent>
</Card>
</div>
</div>


<div class="grid gap-6">

</div>
</template>

<script lang="ts">
import { defineComponent, inject, ref } from 'vue'
import { FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
import { User } from '@/views/auth/User'
import { Input } from '@/components/ui/input'
import { Facebook, Github, Loader2 } from 'lucide-vue-next'
import * as z from 'zod'
import { toTypedSchema } from '@vee-validate/zod'
import { useForm } from 'vee-validate'
import { Button } from '@/components/ui/button'
import ThirdForm from '@/views/auth/components/ThirdForm.vue'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
export default defineComponent({
name: 'FormWithValidate',
components: {
CardContent, Card, CardDescription, CardHeader, CardTitle,
ThirdForm,
Button,
Input,
FormControl, FormField, FormItem, FormLabel, FormMessage,
Loader2, Github, Facebook
},
setup()
{
const $t: any = inject('$t')
let loading = ref(false)
const formState = ref<User>({username: undefined, password: undefined})
const validator = z
.object({
username: z.string({required_error: $t('user.validator.usernameRequired')})
.min(2, $t('user.validator.usernameLengthLeast'))
.max(20, $t('user.validator.usernameLengthMost')),
password: z.string({required_error: $t('user.validator.passwordRequired')})
.min(6, $t('user.validator.passwordLengthLeast'))
.max(20, $t('user.validator.passwordLengthMost'))
})
const {handleSubmit} = useForm({
validationSchema: toTypedSchema(validator)
})
const onSubmit = handleSubmit(() => {
loading.value = true
setTimeout(() => {
loading.value = false
}, 3000)
})
return {
loading,
formState,
onSubmit
}
}
})
</script>

0 comments on commit be20067

Please sign in to comment.