You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Cristian Trifan edited this page Jan 29, 2015
·
2 revisions
You can override the default message for a validation on a per property basis. Here is how you configure the extend call:
ko.validation.rules['mustEqual']={validator: function(val,otherVal){returnval===otherVal;},message: 'The field must equal {0}'};ko.validation.registerExtenders();// If not modifying the default message, you would call like this:// viewModel.PercentSum.extend({ mustEqual: 100 });// If you want to override the message, you need to move // your validation parameter into the "params" fieldviewModel.PercentSum.extend({mustEqual: {params: 100,message: "Percentages must add to 100"}});
Starting with version 2.0.0 the validation message can contain multiple placeholders {0}. For instance, the following custom rule takes advantage of this feature.
ko.validation.rules.between={validator: function(value,params){varmin=params[0];varmax=params[1];value=parseInt(value,10);if(!isNaN(value)){returnvalue>=min&&value<=max;}returnfalse;},message: 'Value must be between {0} and {1}'};ko.validation.registerExtenders();// Use the ruleviewModel.someValue.extend({between: [10,100]});