Comparing two fields with knockout verification

I have a viewmodel for the form I'm trying to add validation to using knockout validation.

fromDate: ko.observable(
            moment().subtract('days', 1).startOf('day').format(dateFormat)
          ),
toDate: ko.observable(
            moment().startOf('day').format(dateFormat)
        ),

I need to make sure that the date from the date is less than today. It seems I can’t get any form of custom validator to get a link to the second observable. I need something like:

toDate: ko.observable(moment().startOf('day').format(dateFormat)).extend({
          validation: {
            validator: function (val, someOtherVal) {
                return moment(val) >= moment(someOtherVal);
            },
            message: 'Must be greater or equal to From Date',
            params: viewModel.fromDate()
          }
        }),

Any ideas?

Update

I'm sure I already tried this, but moving the extension method to the onload function works.

$(function () {
    ko.validation.configure({ decorateElement: true });

    viewModel.toDate.extend({
    validation: {
            validator: function (val, someOtherVal) {
                return moment(val) >= moment(viewModel.fromDate());
            },
            message: 'To date must be greater than from date',
        }
    });

    ko.applyBindings(viewModel);
});
+5
source share
4 answers

. push, : https://github.com/ericmbarnard/Knockout-Validation/pull/217

. ( ). .

function ViewModel() {
    var self = this; // capture this to be able to reference properties

    self.fromDate = ko.observable(
        moment().subtract('days', 1).startOf('day').format(dateFormat)
    );
    self.toDate = ko.observable(
        moment().startOf('day').format(dateFormat)
    ).extend({
      validation: {
        validator: function (val, someOtherVal) {
            return moment(val) >= moment(someOtherVal());
        },
        message: 'Must be greater or equal to From Date',
        params: self.fromDate
      }
    }),
}
+6

, , :

ko.validation.rules['greaterThan'] = {
    validator: function (val, otherVal) {
        return val > otherVal;
    },
    message: 'The field must be greater than {0}'
};

ko.validation.registerExtenders();

fromDate: ko.observable(
            moment().subtract('days', 1).startOf('day').format(dateFormat)
          ),
toDate: ko.observable(
            moment().startOf('day').format(dateFormat)
        ).extend({ greaterThan: fromDate() });

, , fromDate() fromDate, validator otherVal().

+2

, google " ", .

, .

knockoutjs . , , HasMutated() .

  ko.extenders.match = function(target, match_original) {
  target.match_ok = ko.observable();
  function copy(value) {
    target.match_ok(value == match_original());
  }
  function original(value) {
    target.valueHasMutated();
  }
  copy(target());
  target.subscribe(copy);
  match_original.subscribe(original);
  return target;
};

Here is jsfiddle to better illustrate the functionality https://jsfiddle.net/8nugva9f/13/

NTN

0
source
this.fromDate= ko.observable(null).extend({
        date: true,

    });

this.toDate = ko.observable(null).extend({
        date: true,
        min: this.fromDate,

    });

Try it.

-1
source

All Articles