gpt4 book ai didi

javascript - Yup 中的条件验证

转载 作者:行者123 更新时间:2023-11-30 11:16:02 24 4
gpt4 key购买 nike

如何实现 1 个字段的值应始终大于另一个字段的值。

这是我的模式

   value: Yup.number().required(''),
value2: Yup.number().when('value',{
is: (val) => something here
then: Yup.number().required('Required').positive('value should be positive'),
otherwise: Yup.number()
})

我想检查 value2 是否始终为 > 值。如何在 when 条件下访问 value2 的值?

最佳答案

我不确定这样做是否正确,但我正在使用 test .

像这样:

yourField: Yup.string().test('should-be-greather-than-yourField2', 'Should be greather than yourfield 2', function(value) {
const otherFieldValue = this.parent.yourField2

if (!otherFieldValue) {
return true;
}
return value > otherFieldValue;
})

关于javascript - Yup 中的条件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51494109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com