Paritosh Piplewar
Paritosh Piplewar

Reputation: 8132

How to produce one condensed error message with ko.validation.group?

I have many error groups which are executed as per condition to condition.

@errors1 = ko.validation.group [  @foo, @bar ]
@errors2 = ko.validation.group [ @x , @y, @z ]
@errors3 = ko.validation.group [ @apha , @beta, @gama ]

Now for some reason,I want to create a custom <div> error box which will display all its error messages one by one , something like

1. Foo is blank.
2. Bar is not equal to foo. 

How can i ? I think if i am able to iterate observable which are inside that group programtically i might able to produce the above div eaisly ?

PS: i know i can use show error messages with .showAllMessages().

Upvotes: 1

Views: 491

Answers (1)

Wayne Ellery
Wayne Ellery

Reputation: 7958

First you must turn off insertMessages so that messages aren't displayed next to the fields:

ko.validation.configure({
    insertMessages: false
});

Then set each validation group to an observable:

self.errors1 = ko.validation.group([self.firstName, self.lastName]);

Then in your view you can loop through the errors for each validation group at the top like this:

<div data-bind="foreach: errors1">
    <div data-bind="text: $data"></div>
</div>

http://jsfiddle.net/pqd5ds1x/1/

Upvotes: 2

Related Questions