Knockout Validation - How to show error messages

后端 未结 1 1749
小鲜肉
小鲜肉 2020-12-15 19:57

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 kn

1条回答
  •  旧时难觅i
    2020-12-15 20:22

    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

    0 讨论(0)
提交回复
热议问题