hooks.ts 882 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {object, string} from 'zod';
  2. import {useForm} from 'vee-validate';
  3. import {toTypedSchema} from '@vee-validate/zod';
  4. export type FormState = {
  5. name: string;
  6. password: string;
  7. company: string;
  8. };
  9. export function useFormState() {
  10. const {handleSubmit} = useForm<FormState>({
  11. initialValues: {
  12. name: '',
  13. password: '',
  14. company: '',
  15. },
  16. validationSchema: toTypedSchema(
  17. object({
  18. name: string({errorMap: () => ({message: '请输入姓名'})}).min(
  19. 1,
  20. '请输入用户名',
  21. ),
  22. password: string({errorMap: () => ({message: '请输入密码'})}).min(
  23. 1,
  24. '请输入密码',
  25. ),
  26. company: string().optional(),
  27. }),
  28. ),
  29. validateOnMount: false,
  30. });
  31. const onSubmit = handleSubmit(function(value) {
  32. console.log(value);
  33. });
  34. return {onSubmit};
  35. }