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 表单验证正常逻辑的主要内容,如果未能解决你的问题,请参考以下文章