4

I have a rather big knockout model and I want to validate all nested models in it:

self.errors = ko.validation.group(self, { deep: true });

Validator found an error:

> self.errors()
["This field is required."]

I don't know which field of my huge model is invalid. How can I find out it?

Neshta
  • 2,605
  • 2
  • 27
  • 45
  • possible duplicate: http://stackoverflow.com/questions/18129392/how-to-tie-together-ko-validation-errors-with-related-viewmodel-field-names – Beej Mar 31 '16 at 18:19

1 Answers1

3

I guess you should be looking for something like this

// Getting errors
var errors = ko.validation.group(this, {
    deep: true,
    observable: false
});

// New method: getting extended details
var details = errors.getDetails();

for (var i = 0; i < details.length; i++) {
    var d = details[i];

    /*
        Every element contains the following fields:

        "observable" - a reference to the target observable.
        "error" - the error message.
        "rule" - the name of the failed validation rule.
        "data" - an object that contains extension data (provided via "extend" method) for every rule. E.g. "data.required == true".
    */
}

PS: You need to add few lines in your validation file to make getDetails() work i.e which may not be there in validation script file you have .(check reference link & check code)

Reference Here and credits to volpav it helped me long back .

Just incase if someone looking for working sample check here

super cool
  • 6,003
  • 2
  • 30
  • 63
  • Thanks for advice. I have tried to use getDetails using volpav's version of the file, but I have an error "validatables is not defined". – Neshta Dec 05 '14 at 13:30
  • have you copied complete js code in this link ? https://github.com/volpav/Knockout-Validation/blob/master/Src/knockout.validation.js – super cool Dec 05 '14 at 14:51
  • yes, but I had some other errors. I have solve problem with previous js error using context.validatables instead of validatables() The information of the details object help me only by saying another rules of invalid filed. But how can I found it simplier? – Neshta Dec 05 '14 at 15:27
  • can you set me up a sample fiddle with file i mentioned and ur vm i can look into it . cheers – super cool Dec 05 '14 at 15:38
  • 1
    I have found the invalid field by setting a very long string via observable reference and I saw it on the page. Thanks for help =) – Neshta Dec 05 '14 at 15:50