| 123456789101112131415161718192021222324252627282930313233343536373839 |
- import {object, string} from 'zod';
- import {useForm} from 'vee-validate';
- import {toTypedSchema} from '@vee-validate/zod';
- export type FormState = {
- name: string;
- password: string;
- company: string;
- };
- export function useFormState() {
- const {handleSubmit} = useForm<FormState>({
- initialValues: {
- name: '',
- password: '',
- company: '',
- },
- validationSchema: toTypedSchema(
- object({
- name: string({errorMap: () => ({message: '请输入姓名'})}).min(
- 1,
- '请输入用户名',
- ),
- password: string({errorMap: () => ({message: '请输入密码'})}).min(
- 1,
- '请输入密码',
- ),
- company: string().optional(),
- }),
- ),
- validateOnMount: false,
- });
- const onSubmit = handleSubmit(function(value) {
- console.log(value);
- });
- return {onSubmit};
- }
|