Blame view

examples/routers/form.vue 3.5 KB
9f5e2c7e   梁灏   update Form
1
  <template>
ef090131   梁灏   optimize Input va...
2
3
4
5
6
7
8
9
10
11
      <div style="width: 300px;">
          <Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="80">
              <Form-item label="密码" prop="passwd">
                  <Input type="password" v-model="formCustom.passwd"></Input>
              </Form-item>
              <Form-item label="确认密码" prop="passwdCheck">
                  <Input type="password" v-model="formCustom.passwdCheck"></Input>
              </Form-item>
              <Form-item label="年龄" prop="age">
                  <Input type="text" v-model="formCustom.age" number></Input>
fc0c4c78   梁灏   fixed #494
12
13
              </Form-item>
              <Form-item>
ef090131   梁灏   optimize Input va...
14
15
                  <Button type="primary" @click="handleSubmit('formCustom')">提交</Button>
                  <Button type="ghost" @click="handleReset('formCustom')" style="margin-left: 8px">重置</Button>
fc0c4c78   梁灏   fixed #494
16
17
18
              </Form-item>
          </Form>
      </div>
9f5e2c7e   梁灏   update Form
19
20
21
  </template>
  <script>
      export default {
9f5e2c7e   梁灏   update Form
22
          data () {
ef090131   梁灏   optimize Input va...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
              const validatePass = (rule, value, callback) => {
                  if (value === '') {
                      callback(new Error('请输入密码'));
                  } else {
                      if (this.formCustom.passwdCheck !== '') {
                          // 对第二个密码框单独验证
                          this.$refs.formCustom.validateField('passwdCheck');
                      }
                      callback();
                  }
              };
              const validatePassCheck = (rule, value, callback) => {
                  if (value === '') {
                      callback(new Error('请再次输入密码'));
                  } else if (value !== this.formCustom.passwd) {
                      callback(new Error('两次输入密码不一致!'));
                  } else {
                      callback();
                  }
              };
              const validateAge = (rule, value, callback) => {
                  if (!value) {
                      return callback(new Error('年龄不能为空'));
                  }
                  // 模拟异步验证效果
                  setTimeout(() => {
                      if (!Number.isInteger(value)) {
                          callback(new Error('请输入数字值'));
                      } else {
                          if (value < 18) {
                              callback(new Error('必须年满18岁'));
                          } else {
                              callback();
                          }
                      }
                  }, 2000);
              };
  
184dba1c   梁灏   update Form
61
              return {
ef090131   梁灏   optimize Input va...
62
63
64
65
                  formCustom: {
                      passwd: '',
                      passwdCheck: '',
                      age: ''
c3a9f389   梁灏   update Input
66
                  },
ef090131   梁灏   optimize Input va...
67
68
69
70
71
72
73
74
75
                  ruleCustom: {
                      passwd: [
                          { validator: validatePass, trigger: 'blur' }
                      ],
                      passwdCheck: [
                          { validator: validatePassCheck, trigger: 'blur' }
                      ],
                      age: [
                          { validator: validateAge, trigger: 'blur' }
257f80f1   梁灏   support Form
76
                      ]
184dba1c   梁灏   update Form
77
78
                  }
              }
9f5e2c7e   梁灏   update Form
79
          },
184dba1c   梁灏   update Form
80
          methods: {
ef090131   梁灏   optimize Input va...
81
82
83
84
85
86
87
88
89
90
91
              handleSubmit (name) {
                  this.$refs[name].validate((valid) => {
                      if (valid) {
                          this.$Message.success('提交成功!');
                      } else {
                          this.$Message.error('表单验证失败!');
                      }
                  })
              },
              handleReset (name) {
                  this.$refs[name].resetFields();
fc0c4c78   梁灏   fixed #494
92
              }
184dba1c   梁灏   update Form
93
          }
8bca1070   梁灏   update Input on-f...
94
      }
ef090131   梁灏   optimize Input va...
95
  </script>