Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Knockout Validation 3 Parameters

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}'
}
like image 549
Daryl Avatar asked Feb 13 '23 12:02

Daryl


1 Answers

Instead of an Array, you can use an object:

ko.validation.rules['mustLessThanOrEqualConditional'] = {
  validator: function(val, params) {
      if (params.condition) {
          return val <= params.otherVal;
      }
      return true;
  },
  message: 'The field must be <= {0}'
};

model.obs.extend({ mustLessThanOrEqualConditional: {
                       params: { otherVal: 3, condition: 1 != 0 }
                   }
                });

Although in your case, I would suggest using the onlyIf functionnality instead:

ko.validation.rules['mustLessThanOrEqualConditional'] = {
  validator: function(val, params) {
      return val <= params.otherVal;
  },
  message: 'The field must be <= {0}'
};

model.obs.extend({ mustLessThanOrEqualConditional: {
                       onlyIf: function () { /* your test here */ },
                       params: { otherVal: 3 }
                   }
                });

As I am a very nice person, here is the rule I use:

ko.validation.rules['compareTo'] = {
    message: "Compare to",
    validator: function (val, params) {
        if (val === null || params.value() === null)
            return params.allowNull;
        switch (params.way) {
            case "<": return val < params.value();
            case "<=": return val <= params.value();
            case ">": return val > params.value();
            case ">=": return val >= params.value();
            default: throw new Error("params is not well defined");
        }
    }
}

model.obs.extend({ compareTo: { 
                       message:"your message", 
                       params: { 
                                   way: ">=", 
                                   value: model.otherObs, 
                                   allowNull: true 
                       }, 
                       onlyIf: function () { /*your test*/ } 
                    }});
like image 111
GôTô Avatar answered Feb 19 '23 02:02

GôTô