0

I'm using knockout validation plugin. I have a few validation rules for my observable. Is it possible to define which rule failed during the validation?

I tried to use method rules(), but it just returns list of the attached rules, but not a state of the some rule (failed or not):

https://i.stack.imgur.com/5q8Gw.png

I've extended observable in this way:

self.SomeField = ko.observable().extend({
    required: {
        message: "The field is required.",
        params: true
    },
    maxLength: {
        message: "Please enter no more than 300 characters.",
        params: 300 
    }
});

My not validated observable looks like:

https://i.stack.imgur.com/y95Yn.png

Thank you.

Pylyp Lebediev
  • 1,991
  • 4
  • 26
  • 44
  • Why is not enough to have the error messages? What you want to do with the rule itself? – nemesv Jun 05 '13 at 19:46
  • I'm using the next scheme: when my observable is changed I verify is there no error and then save my model, if there is some error - don't save model. Currently I need to allow saving model if required rule failed (only this rule). So I want to filter required rule and even if it's failed - save model, if another rule failed - don't save model. – Pylyp Lebediev Jun 05 '13 at 20:02
  • I'm afraid the current implementation of the validation plugin does not keep track of this information. The only why is to check for the `"The field is required."` error message in the `errors()` collection. However your design is very strange... why is your `SomeField` required if you anyway allow to save it... – nemesv Jun 05 '13 at 20:17
  • Yes, I know it's strange... but our users want to delete information from required field. Such model should be saved and this required field should highlighted as fields with validation error... So the only way to define failed rule is by error message, but this approach isn't really good. Is it possible / necessary to add information about which rules were success / failed in the future versions of the ko validation plugin? – Pylyp Lebediev Jun 05 '13 at 20:32

0 Answers0