Blame view

src/views/demo/form/RuleForm.vue 4.47 KB
陈文彬 authored
1
<template>
2
  <PageWrapper title="表单校验示例">
陈文彬 authored
3
4
5
6
7
8
9
10
11
    <div class="mb-4">
      <a-button @click="validateForm" class="mr-2">手动校验表单</a-button>
      <a-button @click="resetValidate" class="mr-2">清空校验信息</a-button>
      <a-button @click="getFormValues" class="mr-2">获取表单值</a-button>
      <a-button @click="setFormValues" class="mr-2">设置表单值</a-button>
    </div>
    <CollapseContainer title="表单校验">
      <BasicForm @register="register" @submit="handleSubmit" />
    </CollapseContainer>
12
  </PageWrapper>
陈文彬 authored
13
14
15
16
17
18
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { CollapseContainer } from '/@/components/Container/index';
  import { useMessage } from '/@/hooks/web/useMessage';
19
20
  import { PageWrapper } from '/@/components/Page';
陈文彬 authored
21
22
23
24
25
26
27
28
  const schemas: FormSchema[] = [
    {
      field: 'field1',
      component: 'Input',
      label: '字段1',
      colProps: {
        span: 8,
      },
29
      required: true,
陈文彬 authored
30
31
32
33
34
35
36
37
    },
    {
      field: 'field2',
      component: 'Input',
      label: '字段2',
      colProps: {
        span: 8,
      },
38
      required: true,
陈文彬 authored
39
40
41
42
43
44
45
46
    },
    {
      field: 'field3',
      component: 'DatePicker',
      label: '字段3',
      colProps: {
        span: 8,
      },
47
      required: true,
陈文彬 authored
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    },
    {
      field: 'field4',
      component: 'Select',
      label: '字段4',
      colProps: {
        span: 8,
      },
      componentProps: {
        options: [
          {
            label: '选项1',
            value: '1',
            key: '1',
          },
          {
            label: '选项2',
            value: '2',
            key: '2',
          },
        ],
      },
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
      rules: [
        {
          required: true,
          message: '请输入aa',
        },
      ],
    },
    {
      field: 'field44',
      component: 'Input',
      label: '自定义校验',
      colProps: {
        span: 8,
      },
      rules: [
        {
          required: true,
          // @ts-ignore
          validator: async (rule, value) => {
vben authored
89
90
91
            if (!value) {
              return Promise.reject('值不能为空');
            }
92
93
94
95
96
            if (value === '1') {
              return Promise.reject('值不能为1');
            }
            return Promise.resolve();
          },
vben authored
97
          trigger: 'change',
98
99
        },
      ],
陈文彬 authored
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    },
    {
      field: 'field5',
      component: 'CheckboxGroup',
      label: '字段5',
      colProps: {
        span: 8,
      },
      componentProps: {
        options: [
          {
            label: '选项1',
            value: '1',
          },
          {
            label: '选项2',
            value: '2',
          },
        ],
      },
      rules: [{ required: true }],
    },
    {
      field: 'field7',
      component: 'RadioGroup',
      label: '字段7',
      colProps: {
        span: 8,
      },
      componentProps: {
        options: [
          {
            label: '选项1',
            value: '1',
          },
          {
            label: '选项2',
            value: '2',
          },
        ],
      },
      rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
    },
  ];

  export default defineComponent({
146
    components: { BasicForm, CollapseContainer, PageWrapper },
陈文彬 authored
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    setup() {
      const { createMessage } = useMessage();
      const [register, { validateFields, clearValidate, getFieldsValue, setFieldsValue }] = useForm(
        {
          labelWidth: 120,
          schemas,
          actionColOptions: {
            span: 24,
          },
        }
      );
      async function validateForm() {
        try {
          const res = await validateFields();
          console.log('passing', res);
        } catch (error) {
          console.log('not passing', error);
        }
      }
      async function resetValidate() {
        clearValidate();
      }
      function getFormValues() {
        const values = getFieldsValue();
        createMessage.success('values:' + JSON.stringify(values));
      }
      function setFormValues() {
        setFieldsValue({
175
          field1: 1111,
陈文彬 authored
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
          field5: ['1'],
          field7: '1',
        });
      }
      return {
        register,
        schemas,
        handleSubmit: (values: any) => {
          createMessage.success('click search,values:' + JSON.stringify(values));
        },
        getFormValues,
        setFormValues,
        validateForm,
        resetValidate,
      };
    },
  });
</script>