element-ui 表单验证,rules给自定义方法传参数,rules正则验证
el-form表单rules扩展传参
·
1.本质validator其实传入的是一个function,所以可以把这个方法扩展即可
<el-form :model="formData"
:rules="rules">
<el-form-item label="姓名" prop="name">
<el-input v-model="formData.name"></el-input>
</el-form-item>
<el-form-item label="地址" prop="address">
<el-input v-model="formData.address"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model="formData.age"></el-input>
</el-form-item>
<el-form-item label="学校" prop="school">
<el-input v-model="formData.school"></el-input>
</el-form-item>
</el-form>
....
rules: {
// 动态传不定个数的参数,可以往后排
name: [{
validator: (rule, value, callback) => {
valid.notNull(rule, value, callback, this.formData,minSize='',maxSize='',minLen='',maxLen='')
},
trigger: 'blur'
}],
// 不需要传参的
address: [{
validator: valid.notNull,
trigger: 'blur'
}]
// 匹配正则的
address: [{pattern: '^[0-9a-zA-Z_]{1,}$', message: '正则不对'}]
// 最基本的
school: [
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
],
}
更多推荐
已为社区贡献3条内容
所有评论(0)