Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize error message and placement

Currently the Knockout-Validation plugin automatically add this HTML element to my web page:

<span class="validationMessage">This field is required.</span>
  1. I want to change the "This field is required." text.
  2. I want to change the placement of the <span> HTML element.
  3. I want to add a CSS class (.err, specifically) to my textbox so that I can add a red border.

How can that be done with Knockout-Validation?

like image 928
Mr.Wang from Next Door Avatar asked Sep 18 '13 12:09

Mr.Wang from Next Door


People also ask

Why is the appropriate placement of error messages so important?

Finally, the positioning of your error messages are key. This all comes down to good User Experience design practice. Logical positioning not only saves you further detailing the situation, but also allows the user to quickly see where they are going wrong and – that word again – recover!

Where do I put form error message?

Top of Form Validation Vs. Inline Validation The two most common placements for error messages are at the top of the form and inline with erroneous fields.

What is error message explain with example?

An error message is information displayed when an unforeseen problem occurs, usually on a computer or other device. On modern operating systems with graphical user interfaces, error messages are often displayed using dialog boxes.


1 Answers

You can change the default messages for a validation on a per property basis:

test: ko.observable().extend({
        required: {
            params: true,
            message: "This is required"
        }
    })

You can use the validationMessage binding to display the errors wherever you want:

 <span data-bind="validationMessage: test"></span>

And you can use the decorateElement and errorElementClass options (or the other validation bindings) to add some custom classes on your inputs:

ko.validation.init({
    decorateElement: true,
    errorElementClass: 'err'
});

Demo JSFiddle.

like image 57
nemesv Avatar answered Oct 21 '22 05:10

nemesv