使用ember-cp-validations在ember中验证时间

时间:2017-12-11 06:04:41

标签: ember.js ember-cp-validations

我有两个领域,"从时间"和#34;到时间"。我想验证从时间开始应该小于时间和时间应该比从时间更重要。

意味着验证应该依赖于这两个字段。有没有办法验证这种情况?

'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';
    });
}

使用上面的代码,我可以用愉快的路径验证字段。

但是,如果我们将时间设置为11:20am&计时为11:20am,因此错误将显示在“时间”字段中。现在,如果我们从11:19am更改为时间,那么它仍会显示“时间错误”。我希望解决这种情况。

我正在使用ember-cp-validations

感谢。

0 个答案:

没有答案