BasicFormDynamicsRules.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <!-- 动态表单验证 -->
  2. <template>
  3. <!-- 自定义表单 -->
  4. <BasicForm @register="registerForm" style="margin-top: 20px" @submit="handleSubmit" />
  5. </template>
  6. <script lang="ts" setup>
  7. //引入依赖
  8. import { useForm, BasicForm, FormSchema } from '/@/components/Form';
  9. import { duplicateCheck } from '/@/views/system/user/user.api';
  10. //自定义表单字段
  11. const formSchemas: FormSchema[] = [
  12. {
  13. field: 'visitor',
  14. label: '来访人员',
  15. component: 'Input',
  16. //自动触发检验,布尔类型
  17. required: true,
  18. },
  19. {
  20. field: 'accessed',
  21. label: '来访日期',
  22. component: 'DatePicker',
  23. //支持获取当前值判断触发 values代表当前表单的值
  24. required: ({ values }) => {
  25. return !values.accessed;
  26. },
  27. },
  28. {
  29. field: 'phone',
  30. label: '来访人手机号',
  31. component: 'Input',
  32. //动态自定义正则,values: 当前表单的所有值
  33. dynamicRules: ({ values }) => {
  34. //需要return
  35. return [
  36. {
  37. //默认开启表单检验
  38. required: true,
  39. // value 当前手机号输入的值
  40. validator: (_, value) => {
  41. //需要return 一个Promise对象
  42. return new Promise((resolve, reject) => {
  43. if (!value) {
  44. reject('请输入手机号!');
  45. }
  46. //验证手机号是否正确
  47. let reg = /^1[3456789]\d{9}$/;
  48. if (!reg.test(value)) {
  49. reject('请输入正确手机号!');
  50. }
  51. resolve();
  52. });
  53. },
  54. },
  55. ];
  56. },
  57. },
  58. ];
  59. /**
  60. * BasicForm绑定注册;
  61. */
  62. const [registerForm] = useForm({
  63. //注册表单列
  64. schemas: formSchemas,
  65. showResetButton: false,
  66. labelWidth: '150px',
  67. submitButtonOptions: { text: '提交', preIcon: '' },
  68. actionColOptions: { span: 17 },
  69. });
  70. /**
  71. * 提交事件
  72. */
  73. function handleSubmit(values: any) {}
  74. </script>
  75. <style scoped></style>