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);
});
source
share