hooks.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {toTypedSchema} from '@vee-validate/zod';
  2. import {useForm} from 'vee-validate';
  3. import {Ref} from 'vue';
  4. import {object, string} from 'zod';
  5. type FormState = {
  6. enterprise: string;
  7. phone: string;
  8. idCardImage: string;
  9. licenseImage: string;
  10. };
  11. const validate = object({
  12. enterprise: string({errorMap: () => ({message: '请输入企业名称'})})
  13. .min(1, '请输入企业名称'),
  14. phone: string({errorMap: () => ({message: '请输入企业名称'})})
  15. .min(1, '请输入企业名称'),
  16. idCardImage: string({errorMap: () => ({message: '请上传法人身份证'})})
  17. .min(1, '请上传法人身份证'),
  18. licenseImage: string({errorMap: () => ({message: '请上传营业执照'})})
  19. .min(1, '请上传营业执照'),
  20. });
  21. export function useFormState(isSuccess: Ref<boolean>) {
  22. const {handleSubmit, handleReset} = useForm<FormState>({
  23. initialValues: {
  24. enterprise: '',
  25. phone: '',
  26. idCardImage: '',
  27. licenseImage: '',
  28. },
  29. validationSchema: toTypedSchema(validate),
  30. });
  31. const onSubmit = handleSubmit(function(value) {
  32. isSuccess.value = true;
  33. });
  34. return {onSubmit, handleReset};
  35. }