vincentsty
vincentsty

Reputation: 3231

ko validation Maxlength only if

For knockout required validation, the onlyIf is used in this way. How about maxLength validation? I have tried as follow but not working. Hope someone could help on this.

self.postalCode.extend({
  required: {
    message: "Postal Code is required",
    onlyIf: function() {
      return self.noPostalCode() === false;
    }
  }
});

// Not Working
self.postalCode.extend({
  maxLength: 3 {
    message: "Postal Code is required",
    onlyIf: function() {
      return self.noPostalCode() === false;
    }
  }
});

Upvotes: 3

Views: 5818

Answers (1)

super cool
super cool

Reputation: 6045

All you need to do is set maxLength property value using params

viewModel:

self.postalCode.extend({
      maxLength: {
          message: "Postal Code is required",
          onlyIf: function () {
              return self.noPostalCode() === false;
           },
         params: 3
      }
 });

working sample here

Upvotes: 7

Related Questions