标签:bsp reset require 关系 BMI method page 提交 ===
基本上按照文档上提供的方式做就没啥大问题 , 我遇到的问题是 , 自定义以后不显示小红星了
<el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm"> <el-form-item label="密码" prop="pass"> <el-input type="password" v-model="ruleForm2.pass" autocomplete="off"></el-input> </el-form-item> <el-form-item label="确认密码" prop="checkPass"> <el-input type="password" v-model="ruleForm2.checkPass" autocomplete="off"></el-input> </el-form-item> <el-form-item label="年龄" prop="age"> <el-input v-model.number="ruleForm2.age"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm(‘ruleForm2‘)">提交</el-button> <el-button @click="resetForm(‘ruleForm2‘)">重置</el-button> </el-form-item> </el-form> <script> export default { data() { var checkAge = (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(); } } }, 1000); }; var validatePass = (rule, value, callback) => { if (value === ‘‘) { callback(new Error(‘请输入密码‘)); } else { if (this.ruleForm2.checkPass !== ‘‘) { this.$refs.ruleForm2.validateField(‘checkPass‘); } callback(); } }; var validatePass2 = (rule, value, callback) => { if (value === ‘‘) { callback(new Error(‘请再次输入密码‘)); } else if (value !== this.ruleForm2.pass) { callback(new Error(‘两次输入密码不一致!‘)); } else { callback(); } }; return { ruleForm2: { pass: ‘‘, checkPass: ‘‘, age: ‘‘ }, rules2: { pass: [ { validator: validatePass, trigger: ‘blur‘ } ], checkPass: [ { validator: validatePass2, trigger: ‘blur‘ } ], age: [ { validator: checkAge, trigger: ‘blur‘ } ] } }; }, methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { alert(‘submit!‘); } else { console.log(‘error submit!!‘); return false; } }); }, resetForm(formName) { this.$refs[formName].resetFields(); } } } </script>
我只是照着改了一下
let validatePass = (rule, value, callback) => { console.log(rule); console.log(value); console.log(callback); if (!value) { return callback(new Error("请填写公司名称")); } if (this.form.id) { callback(); return true; } readScmSupplierPage({ name: this.form.name ,type:‘2‘}) .then(res => { if (res.data.length > 0) { callback(new Error("名称重复")); } else { callback(); } }) .catch(err => { console.log(err); }); };
基本上和自定义没啥关系
rules: { // name: [{ required: true, message: "请输入公司名称", trigger: "blur" }], name: [{ required: true, validator: validatePass, trigger: "blur" }], abbreviation: [ { required: true, message: "请输入公司简称", trigger: "blur" } ] },
只是我发现如果自定义了 , 在这个地方加required: true, 是不起作用的, 必须在form表单上面加
<el-form-item label="公司名称" :label-width="formLabelWidth" prop="name" required> <el-input v-model="form.name"></el-input> </el-form-item>
就这样小红星星就出现啦
element-ui 自定义表单验证 , 但是不出现小红心了
标签:bsp reset require 关系 BMI method page 提交 ===
原文地址:https://www.cnblogs.com/sunjinggege/p/9768125.html