vue view 表单验证正常逻辑

Posted superDragon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue view 表单验证正常逻辑相关的知识,希望对你有一定的参考价值。

<template>
    <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
        <FormItem prop="user">
             <InputNumber :max="100" :min="0" step="1" v-model="formInline.user"></InputNumber>
        </FormItem>
        <FormItem prop="password">
           <InputNumber :max="100" :min="0" step="1" v-model="formInline.password"></InputNumber>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit(‘formInline‘)">登录</Button>
        </FormItem>
    </Form>
</template>
<script>
    export default {
        data () {
           const user = (rule, value, callback) => {
                if (value == 0) {
                    callback(new Error(‘用户名不能为空‘));
                } else {
                    callback();
                }
            };
            const password = (rule, value, callback) => {
                if (value == 0) {
                    callback(new Error(‘密码不能为空‘));
                } else {
                    callback();
                }
            };
            return {
                formInline: {
                    user: 0,
                    password: 0
                },
                ruleInline: {
                    user: [
                        { required: true, validator: user, trigger: ‘change‘ }
                    ],
                    password: [
                        { required: true, validator: password, trigger: ‘change‘ },
                    ]
                }
            }
        },
        methods: {
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$Message.success(‘Success!‘);
                    } else {
                        this.$Message.error(‘Fail!‘);
                    }
                })
            }
        }
    }
</script>

 

<template>
<Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
<FormItem prop="user">
<InputNumber :max="100" :min="0" step="1" v-model="formInline.user"></InputNumber>
</FormItem>
<FormItem prop="password">
<InputNumber :max="100" :min="0" step="1" v-model="formInline.password"></InputNumber>
</FormItem>
<FormItem>
<Button type="primary" @click="handleSubmit(‘formInline‘)">登录</Button>
</FormItem>
</Form>
</template>
<script>
export default {
data () {
const user = (rule, value, callback) => {
if (value == 0) {
callback(new Error(‘用户名不能为空‘));
} else {
callback();
}
};
const password = (rule, value, callback) => {
if (value == 0) {
callback(new Error(‘密码不能为空‘));
} else {
callback();
}
};
return {
formInline: {
user: 0,
password: 0
},
ruleInline: {
user: [
{ required: true, validator: user, trigger: ‘change‘ }
],
password: [
{ required: true, validator: password, trigger: ‘change‘ },
]
}
}
},
methods: {
handleSubmit(name) {
this.$refs[name].validate((valid) => {
if (valid) {
this.$Message.success(‘Success!‘);
} else {
this.$Message.error(‘Fail!‘);
}
})
}
}
}
</script>

以上是关于vue view 表单验证正常逻辑的主要内容,如果未能解决你的问题,请参考以下文章

mvc3 + jQuery 验证:如何捕获错误或正常事件

vue项目:新增时清除表单验证注意事项

vue element-ui 的form表单验证rules

简易登录表单的制作,包括用户名密码随机验证码(代码完整,复制即用)

Vue处理表单校验

vue项目中填写一个内容后另一项内容为必填时表单验证