1.基礎姿式/自定義姿式bash
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
<el-form-item label="活動名稱" prop="name">
<el-input v-model="ruleForm.name"></el-input>
</el-form-item>
<el-form-item label="年齡" prop="age">
<el-input v-model.number="ruleForm.age"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">當即建立</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
<script>
var checkAge = (rule, value, callback) => {
//console.log(rule.max_age)
if (!value) {
return callback(new Error('年齡不能爲空'));
}
if (!Number.isInteger(value)) {
callback(new Error('請輸入數字值'));
} else {
if (value < rule.max_age) {
callback(new Error('必須年滿18歲'));
} else {
callback();
}
}
};
export default {
data() {
return {
ruleForm: {
name: '',
age:''
},
rules: {
name: [
{ required: true, message: '請輸入活動名稱', trigger: 'blur' },
{ min: 3, max: 5, message: '長度在 3 到 5 個字符', trigger: 'blur' }
],
age: [
{max_age:18, validator: checkAge, trigger: 'blur' }// checkAge自定義規則函數
]
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!')
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
</script>
複製代碼
在el-form上綁定rules,rlues爲校驗規則,在el-form-item上綁定對應的prop。 自定義規則,validator:checkAge,自定義規則的函數。 不囉嗦,詳細的可查看element文檔http://element-cn.eleme.io/#/zh-CN/component/form。 2.行間校驗dom
<el-form :model="numberValidateForm" ref="numberValidateForm" label-width="100px" class="demo-ruleForm">
<el-form-item
label="年齡"
prop="age"
:rules="[ { required: true, message: '年齡不能爲空'}, { type: 'number', message: '年齡必須爲數字值'} ]">
<el-input type="age" v-model.number="numberValidateForm.age" autocomplete="off"></el-input>
</el-form-item>
複製代碼
可將rule寫在各自的el-form-item上 3.行間自定義校驗函數
<el-form :model="numberValidateForm" ref="numberValidateForm" label-width="100px" class="demo-ruleForm">
<el-form-item
label="年齡"
prop="age"
:rules="[ {max_age:18, validator: checkAge, trigger: 'blur' } ]">
<el-input type="age" v-model.number="numberValidateForm.age" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<script>
export default {
data() {
...
},
methods: {
checkAge(rule, value, callback){
//自定義校驗規則
//console.log(rule.max_age)
if (!value) {
return callback(new Error('年齡不能爲空'));
}
if (!Number.isInteger(value)) {
callback(new Error('請輸入數字值'));
} else {
if (value < rule.max_age) {
callback(new Error('必須年滿18歲'));
} else {
callback();
}
}
},
......
}
}
</script>
複製代碼
4.行間循環自定義校驗ui
<el-form>
<el-form-item
v-for="(domain, index) in dynamicValidateForm.domains"
:label="'域名' + index"
:key="domain.key"
:prop="`domains.${index}.value`" //綁定的prop
:rules="[ { required: true, message: '域名不能爲空', trigger: 'blur' }, {reg:/^--------$/, validator: checkDomain, trigger: 'blur' } ]"
>
</el-form-item>
</el-form>
<script>
export default {
data() {
return {
dynamicValidateForm: {
domains: [
{ value: ''},
{ value: ''},
{ value: ''},
]
}
};
},
methods: {
checkDomain(rule, value, callback){
//自定義校驗規則
......
},
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
}
});
}
}
}
</script>
複製代碼