2 回答

TA贡献1813条经验 获得超2个赞
number.max不能引用其他字段并在验证时用它计算。
如果你想这样做,你需要使用mixed.test.
这是一个例子。
tips: number()
.min(0, `Minimum tip is $0`)
.test({
name: 'max',
exclusive: false,
params: { },
message: '${path} must be less than 10% of the price',
test: function (value) {
// You can access the price field with `this.parent`.
return value <= parseFloat(this.parent.price * 0.1)
},
}),
检查并尝试它是如何工作的。

TA贡献1906条经验 获得超10个赞
如果您不想使用this.parent访问其他属性值,您可以使用context。
tips: number()
.min(0, `Minimum tip is $0`)
.test(
'max',
'${path} must be less than 10% of the price',
(value, context) => value <= parseFloat(context.parent.price * 0.1),
),
// OR
tips: number()
.min(0, `Minimum tip is $0`)
.test({
name: 'max',
exclusive: false,
params: { },
message: '${path} must be less than 10% of the price',
test: (value, context) => value <= parseFloat(context.parent.price * 0.1),
}),
添加回答
举报