How do I know which field is invalid on a knockout check?

I have a rather large knockout model and I want to check all nested models in it:

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

      

The validator encountered an error:

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

      

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

+3


source to share


1 answer


I think 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 a few lines to the validation file to make the job work getDetails()

i.e. which may be missing from the validation script file (see link link and validation code)



Link Here and credits to volpav, this helped me a long time.

Just add if anyone is looking working sample

check here

+4


source







All Articles