Grails: Listing the available message codes for errors

The following snippet is often used to list all errors of a form.

<g:eachError bean="${contactForm}">
     <li><g:message error="${it}" /></li>
</g:eachError>

If you do not specify any custom messages you will get the default ones. For example
Property [name] of class [class com.myCompany.ContactForm] cannot be blank

To overwrite the default message you have in Grails several possibilities which are too many to remember. So I’m using the following snippet to show which is the current error messsage and which codes would be possible.

<g:eachError bean="${contactForm}" var="error">
    ${error.field}: <g:message error="${error}" />
    <ul>
    <g:each in="${error.codes}" var="code">
        <li>${code}</li>
    </g:each>
    </ul>
</g:eachError>

Example output:

name: Property [name] of class [class com.myCompany.ContactFormCommand] cannot be blank

  • com.myCompany.ContactFormCommand.name.blank.error.com.myCompany.ContactFormCommand.name
  • com.myCompany.ContactFormCommand.name.blank.error.name
  • com.myCompany.ContactFormCommand.name.blank.error.java.lang.String
  • com.myCompany.ContactFormCommand.name.blank.error
  • contactFormCommand.name.blank.error.com.myCompany.ContactFormCommand.name
  • contactFormCommand.name.blank.error.name
  • contactFormCommand.name.blank.error.java.lang.String
  • contactFormCommand.name.blank.error
  • com.myCompany.ContactFormCommand.name.blank.com.myCompany.ContactFormCommand.name
  • com.myCompany.ContactFormCommand.name.blank.name
  • com.myCompany.ContactFormCommand.name.blank.java.lang.String
  • com.myCompany.ContactFormCommand.name.blank
  • contactFormCommand.name.blank.com.myCompany.ContactFormCommand.name
  • contactFormCommand.name.blank.name
  • contactFormCommand.name.blank.java.lang.String
  • contactFormCommand.name.blank
  • blank.com.myCompany.ContactFormCommand.name
  • blank.name
  • blank.java.lang.String
  • blank

So now you can easily choose the best message code.

References