自增input框的一些校验
结构:一行为两个input框,左侧为充值金额,右侧为获得金额。
校验规则:充值金额不能有重复,并且同一行的获得金额不得大于充值金额。
const benefitvValidator = (rule, value, callback) => {
// 获得金额校验
const id = rule.field.split('.')[1]
const value1 = field.getValue(`principal.${id}`)
if (!value) {
field.setError(`realAmount.${id}`, '')
setTimeout(() => {
field.setError(`realAmount.${id}`, '请输入获得金额!')
}, 0)
return callback()
} else {
if (Number(value) < Number(value1)) {
setTimeout(() => {
field.setError(`realAmount.${id}`, '获得金额需大于充值金额!')
}, 0)
}
const tempList = `${value}`.split('.')
if (value > 100000) {
return callback('充值金额最大为100000元!')
} else if (tempList[1] && tempList[1].length > 2) {
return callback('最多支持两位小数!')
} else {
return callback()
}
}
}
<Input
addonTextAfter="元"
style={{ width: '160px' }}
name={`principal.${x.id}`}
defaultValue={x.principalYuan}
disabled={x.principal ? true : false}
onChange={() => {
// 横向校验 realAmount
const value1 = field.getValue(`principal.${x.id}`)
const value2 = field.getValue(`realAmount.${x.id}`)
if (Number(value2) < Number(value1) && value2 !== '') {
field.setError(
`realAmount.${x.id}`,
'获得金额需大于充值金额!'
)
} else {
field.setError(`realAmount.${x.id}`, '')
}
// 纵向校验
const values = field.getValues()
const arr = []
const arr2 = []
const temp = {}
const keys = Object.keys(values).filter(key =>
key.includes('principal')
)
keys.forEach(key => {
const val = Arithmetic.yuanToCent(values[key])
console.log(val,'lllllll');
if (!temp[val]) {
temp[val] = []
}
if (val !== '') {
temp[val].push(key)
}
})
Object.values(temp).forEach(val => {
if (val.length > 1) {
arr.push(...val)
} else {
arr2.push(...val)
}
})
const errors = {}
arr.forEach(key => {
errors[key] = '不同档位的重置金额不得相同!'
})
arr2.forEach(key => {
errors[key] = ''
})
setTimeout(() => {
field.setErrors(errors)
}, 0)
}}
/>