简体   繁体   English

如何知道敲除验证中哪个字段无效?

[英]How to know which field is invalid in knockout validation?

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: Validator发现错误:

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

I don't know which field of my huge model is invalid. 我不知道我的大型模型的哪个领域无效。 How can I find out it? 我怎么能找到它?

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 ie which may not be there in validation script file you have .(check reference link & check code) PS:您需要在验证文件中添加几行以使getDetails()工作,即您可能没有验证脚本文件中的那些。(检查参考链接和检查代码)

Reference Here and credits to volpav it helped me long back . 参考这里和volpav的信用它帮助我很久以前。

Just incase if someone looking for working sample check here 如果有人在这里寻找working sample请加入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM