Iview验证相关

作者: 凌雲木 | 来源:发表于2019-04-23 00:03 被阅读3次

    1 ruleValidate 有多余的时候

    • ruleValidate 需要验证的字段多,而页面没有显示那么多字段,在提交的时候,只要能满足,页面显示要验证的字段就能提交成功
    <template>
        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
           
            <FormItem label="Hobby" prop="interest">
                <CheckboxGroup v-model="formValidate.interest">
                    <Checkbox label="Eat"></Checkbox>
                    <Checkbox label="Sleep"></Checkbox>
                    <Checkbox label="Run"></Checkbox>
                    <Checkbox label="Movie"></Checkbox>
                </CheckboxGroup>
            </FormItem>
            <FormItem label="Desc" prop="desc">
                <Input v-model="formValidate.desc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="Enter something..."></Input>
            </FormItem>
            <FormItem>
                <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
                <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
            </FormItem>
        </Form>
    </template>
    <script>
        export default {
            data () {
                return {
                    formValidate: {
                        name: '',
                        mail: '',
                        city: '',
                        gender: '',
                        interest: [],
                        date: '',
                        time: '',
                        desc: ''
                    },
                    ruleValidate: {
                        name: [
                            { required: true, message: 'The name cannot be empty', trigger: 'blur' }
                        ],
                        mail: [
                            { required: true, message: 'Mailbox cannot be empty', trigger: 'blur' },
                            { type: 'email', message: 'Incorrect email format', trigger: 'blur' }
                        ],
                        city: [
                            { required: true, message: 'Please select the city', trigger: 'change' }
                        ],
                        gender: [
                            { required: true, message: 'Please select gender', trigger: 'change' }
                        ],
                        interest: [
                            { required: true, type: 'array', min: 1, message: 'Choose at least one hobby', trigger: 'change' },
                            { type: 'array', max: 2, message: 'Choose two hobbies at best', trigger: 'change' }
                        ],
                        date: [
                            { required: true, type: 'date', message: 'Please select the date', trigger: 'change' }
                        ],
                        time: [
                            { required: true, type: 'string', message: 'Please select time', trigger: 'change' }
                        ],
                        desc: [
                            { required: true, message: 'Please enter a personal introduction', trigger: 'blur' },
                            { type: 'string', min: 20, message: 'Introduce no less than 20 words', trigger: 'blur' }
                        ]
                    }
                }
            },
            methods: {
                handleSubmit (name) {
                    this.$refs[name].validate((valid) => {
                        if (valid) {
                            this.$Message.success('Success!');
                        } else {
                            this.$Message.error('Fail!');
                        }
                    })
                },
                handleReset (name) {
                    this.$refs[name].resetFields();
                }
            }
        }
    </script>
    
    yyyyyyyyyyyyyyy.png

    2 自定义验证

    <template>
        <Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="180">
            <FormItem label="Password" prop="passwd">
                <Input type="password" v-model="formCustom.passwd"></Input>
            </FormItem>
            <FormItem label="Confirm" prop="passwdCheck">
                <Input type="password" v-model="formCustom.passwdCheck"></Input>
            </FormItem>
            <FormItem label="Age" prop="age">
                <Input type="text" v-model="formCustom.age" number></Input>
            </FormItem>
            <FormItem>
                <Button type="primary" @click="handleSubmit('formCustom')">Submit</Button>
                <Button @click="handleReset('formCustom')" style="margin-left: 8px">Reset</Button>
            </FormItem>
        </Form>
    </template>
    <script>
        export default {
            data () {
                const validatePass = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('Please enter your password'));
                    } else {
                        if (this.formCustom.passwdCheck !== '') {
                            // 对第二个密码框单独验证
                            this.$refs.formCustom.validateField('passwdCheck');
                        }
                        callback();
                    }
                };
                const validatePassCheck = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('Please enter your password again'));
                    } else if (value !== this.formCustom.passwd) {
                        callback(new Error('The two input passwords do not match!'));
                    } else {
                        callback();
                    }
                };
                const validateAge = (rule, value, callback) => {
                    if (!value) {
                        return callback(new Error('Age cannot be empty'));
                    }
                    // 模拟异步验证效果
                    setTimeout(() => {
                        if (!Number.isInteger(value)) {
                            callback(new Error('Please enter a numeric value'));
                        } else {
                            if (value < 18) {
                                callback(new Error('Must be over 18 years of age'));
                            } else {
                                callback();
                            }
                        }
                    }, 1000);
                };
                
                return {
                    formCustom: {
                        passwd: '',
                        passwdCheck: '',
                        age: ''
                    },
                    ruleCustom: {
                        passwd: [
                            { required: true, message: '不能为空', validator: validatePass, trigger: 'blur' }
                        ],
                        passwdCheck: [
                            { validator: validatePassCheck, trigger: 'blur' }
                        ],
                        age: [
                            { validator: validateAge, trigger: 'blur' }
                        ]
                    }
                }
            },
            methods: {
                handleSubmit (name) {
                    this.$refs[name].validate((valid) => {
                        if (valid) {
                            this.$Message.success('Success!');
                        } else {
                            this.$Message.error('Fail!');
                        }
                    })
                },
                handleReset (name) {
                    this.$refs[name].resetFields();
                }
            }
        }
    </script>
    
    
    如图: zzzzzzzz.png

    相关文章

      网友评论

        本文标题:Iview验证相关

        本文链接:https://www.haomeiwen.com/subject/vwnpgqtx.html