Knockout Validation - How to show error messages

一笑奈何 提交于 2019-11-27 15:51:32

问题


We're using Knockout.js and the Knockout-validation plugin. When a user returns to a page that has validation errors, we want the error messages to display. Does anyone know if it's possible to trigger knockout validation without actually changing the bound answer?


回答1:


The solution is to call showAllMessages. If the view model has nested observables, be sure to set ko.validation.configure to use deep grouping because the default value is false.

Example:

viewModel.save = function()
{
    var result = ko.validation.group(viewModel, {deep: true});
    if (!viewModel.isValid()) 
    {
        alert("Please fix all errors before preceding");
        result.showAllMessages(true);

        return false;
    }

    //actually save stuff, call ajax, submit form, etc
}

Alternatively, you can replace !viewModel.isValid() with result().length > 0



来源:https://stackoverflow.com/questions/12098029/knockout-validation-how-to-show-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!