Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout Validation - How to show error messages

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?

like image 634
CBlack Avatar asked Aug 23 '12 18:08

CBlack


People also ask

What is validation error message?

The Validation Error Message property lets you define a custom error message to display if the validation checks specified in the Validation (Regex) fails.


1 Answers

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

like image 123
CBlack Avatar answered Sep 29 '22 04:09

CBlack