我有两个字段,“从时间”和“到时间”。我想验证 From time 应该小于 To time 并且 To time 应该大于 From time。
意味着验证应该取决于这两个字段。有什么方法可以验证这种情况吗?
'fieldData.FROM_TIME': function(keyToGetData){
return validator(function(value, options, model) {
let fromTime = value;
let toTime = model.get(keyToGetData);
if(fromTime){
let fromHours = fromTime.hours;
let fromMins = fromTime.minutes;
// make validation only if toTime is there
if(toTime){
let toHours = toTime.hours;
let toMins = toTime.minutes;
if(fromHours > toHours || ( fromHours===toHours && fromMins > toMins) ){
return 'From time must be earlier than To time.';
}
}
return true;
}
return 'This field can not be blank';
});
},
'fieldData.TO_TIME': function(keyToGetData){
return validator(function(value, options, model) {
let fromTime = model.get(keyToGetData);
let toTime = value;
if(toTime){
let toHours = toTime.hours;
let toMins = toTime.minutes;
// make validation only if fromTime is there
if(fromTime){
let fromHours = fromTime.hours;
let fromMins = fromTime.minutes;
if(fromHours > toHours || (fromHours===toHours && fromMins >= toMins) ){
return 'To time must be later than From time.';
}
}
return true;
}
return 'This field can not be blank';
});
}
使用上面的代码,我可以验证具有快乐路径的字段。
但是,如果我们将 From time 设置为11:20am
& To time as11:20am
那么错误将显示在 To time 字段上。现在,如果我们更改 From 时间,11:19am
它仍然会显示 To time 错误。我想要解决这种情况。
我正在使用ember-cp-validations。
谢谢。