Specify custom error messages
Field names
You can set the field name through HTML in the same way as described in the field names feature and still benefit when coding the validations. You can also set the field names programatically:
"name".is("required").withName("First name");
Custom error messages per validator
Setting custom error messages for a field and specific validator is something that cannot be done at all through the HTML extension, but is quite simple with the DSL API:
"name".is("required").explain("Please enter name");
Note that order matters when adding (and explaining) several validators on the same field:
"name".is("required").explain("Please enter name").
andHas("min-length", 4).explain("Atleast 4 characters");
The message passed to the explain method will apply to the
current validator.
Custom error messages
Like with the HTML extension, you can set custom error messages that will cover the field and any number of validators:
"name".is("required").andHas("min-length", 4).
help("Please enter name of atleast 4 characters");
Note that the help method will cause the field to always generate
only this message no matter how many validators fail for the given field. This
also means that messages added with explain are overridden by the
help method.