Grails - Validation and Internationalization



Description

Grails displays the error messages by using the FieldError class from message bundles with i18n support. Grails supports internationalization by using the Locale class which specifies the geographical, political, or cultural region. A Locale includes language and country code, for instance "en_US" is the code for US English.

Grails provides the below error codes, if you ignore a constraint:

S.N.ConstraintError Code
1 blank className.propertyName.blank
2 creditCard className.propertyName.creditCard.invalid
3 email className.propertyName.email.invalid
4 inList className.propertyName.not.inList
5 matches className.propertyName.matches.invalid
6 max className.propertyName.max.exceeded
7 maxSize className.propertyName.maxSize.exceeded
8 min className.propertyName.min.notmet
9 minSize className.propertyName.minSize.notmet
10 notEqual className.propertyName.notEqual
11 nullable className.propertyName.nullable
12 range className.propertyName.range.toosmall or className.propertyName.range.toobig
13 size className.propertyName.size.toosmall or className.propertyName.size.toobig
14 unique className.propertyName.unique
15 url className.propertyName.url.invalid
16 validator classname.propertyName. + String returned by Closure

For instance:

Consider the blank constraint which could be written as team.name.blank that can be specified in your grails-app/i18n/messages.properties file to display the following message:

team.name.blank=please enter the name!!!

Displaying Messages

The messages can be displayed from the message bundles by using the message tag.

<g:hasErrors bean="${team}">
  <ul>
   <g:eachError var="msgerror" bean="${team}">
       <li><g:message error="${msgerror}" /></li>
   </g:eachError>
  </ul>
</g:hasErrors>

In the above code snippet, the message tag is used within the body of the eachError tag along with an error argument to display the message for the given error.

Advertisements