I want to create a custom knockout validation rule that would take in the current observable's value, the value to compare it to, and a conditional.
All of the examples I've seen on the web have only two parameters passed into the custom validation.
I have made a workaround to pass an array with the value to compare and the conditional, but this is a bit clunky. My message format outputs the values in the array as well instead of just the otherVal
.
For Example:
/*
* Compares two values only if the specified condition is true.
* Parameters:
* array[0] - otherVal
* array[1] - conditional
*
* Returns:
* true if the condition is false.
* true if the condition is true AND val is lessThanOrEqualTo otherVal
* false otherwise
*
* Usage:
* Used to validate observables conditionally.
*
* Example:
* self.salary = ko.observable().extend({
mustLessThanOrEqualConditional: [100, isFieldEditable]})
*/
ko.validation.rules['mustLessThanOrEqualConditional'] = {
validator: function(val, array) {
if (array[1]) {
return val <= array[0];
}
return true;
},
message: 'The field must be <= {0}'
};
Is there a way to pass three parameters into the validation function such as:
ko.validation.rules['mustLessThanOrEqualConditional'] = {
validator: function(val, otherVal, condition) {
if(condition){
return val <= otherVal;
}
return true;
},
message: 'The field must be <= {0}'
}
Instead of an
Array
, you can use an object:Although in your case, I would suggest using the
onlyIf
functionnality instead:As I am a very nice person, here is the rule I use: