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

时间:2014-12-05 11:25:48

标签: javascript knockout.js knockout-validation

我有一个相当大的淘汰模型,我想验证其中的所有嵌套模型:

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

Validator发现错误:

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

我不知道我的大型模型的哪个区域无效。我怎么能找到它?

1 个答案:

答案 0 :(得分:4)

我猜你应该找这样的东西

// 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:您需要在验证文件中添加几行才能使getDetails()正常工作,即您在验证脚本文件中可能没有。(查看参考链接和检查代码)

参考Here并归功于volpav,它帮助了我很久。

如果有人在寻找working sample,请检查 here