javascript - 是的,条件验证
问题描述
如何实现条件,例如 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 是否始终为 > value。如何在 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;
})
推荐阅读
- wpf - WPF DataGridComboBoxColumn 绑定
- php - PHP会将文件保存到一个文件夹而不是另一个文件夹
- java - 带有子查询抛出错误的准备好的语句
- java - 类或接口除外
- squid - 具有多个虚拟 IP 的 Squid 代理集群
- javascript - Bootstrap Vue perPage for custom columns or rows
- themes - 如何更改 Contempo Blogger 主题中的文本?
- dart - OverflowBox,溢出的部分无法响应按钮?
- django - 找不到满足django要求的版本(来自版本:)没有为django找到匹配的发行版
- python - 非侵入式 conda 安装