当Form表单添加rules格式校验时,我们一般会采取默认iview form表单校验,这导致一些数字类型(number)的数据验证失效。
后端存储的数据类型都是number类型,不要以前端传什么存什么为主哦
<Form ref="formValidate" :model="form1" :rules="ruleValidate">
<FormItem label="90分位时效:" prop="percentileAging" :label-width="formNameWidth">
<InputNumber :min="1" v-model.trim="form1.percentileAging">InputNumber>
FormItem>
Form>
以
percentileAging
字段为例,值为数字类型
type: 'number'
类型必须添加,否则你数据校验时表单校验不通过ruleValidate: {
percentileAging: [{ required: true,type: 'number', message: '请输入90分位时效', trigger: 'blur' }]
},
<Form ref="formValidate" :model="form1" :rules="ruleValidate">
<FormItem label="90分位时效:" prop="percentileAging" :label-width="formNameWidth">
<Input v-model.trim="form1.percentileAging" clearable placeholder="请输入时效考核标准">Input>
FormItem>
Form>
js
ruleValidate: {
percentileAging: [{ required: true, message: '请输入90分位时效', trigger: 'blur' }]
},
在点击编辑修改操作时直接赋值
this.openModal('edit', row)
methods: {
// 新增/编辑
openModal(status, row) {
this.form1 = this.$options.data().form1 // 初始化表单
this.modalStatus = status
if (status === 'add') {
this.$refs['formValidate'].resetFields()
this.modal1 = true
} else if (status === 'edit') { // 修改渠道时效维护
// 渲染编辑的表单数据
let tempForm = this.$rowEditFormNumToStr(this.form1, row)
this.form1 = tempForm
setTimeout(() => {
this.modal1 = true
}, 100)
}
},
},
$rowEditFormNumToStr
/**
* row数据编辑 form => row 填 数字类型变字符串类型,表单验证不生效问题
* @param {object} form
* @param {object} row
* @returns {object}
*/
Vue.prototype.$rowEditFormNumToStr = (form, row) => {
Object.keys(form).forEach(key => {
form[key] = typeof row[key] === 'number' && isFinite(row[key]) ? `${row[key]}` : row[key]
})
return form
}