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

Eclipse open GSP-page: Editor could not be initialized

I’m using Eclipse with the SpringSource Tool Suite. After importing a Grails Project, I wanted to open a GSP-page but got the following error in the Editor: Could not open the editor: Editor could not be initialized.

The stacktrace looks as follows:

java.lang.NullPointerException
    at com.springsource.sts.grails.editor.gsp.tags.GSPModelQueryCMProvider.(GSPModelQueryCMProvider.java:53)
    at com.springsource.sts.grails.editor.gsp.tags.GSPModelQueryImpl.(GSPModelQueryImpl.java:36)
    ...
    at org.eclipse.equinox.launcher.Main.main(Main.java:1287)

The error occurs because Eclipse (STS) wants to open GSP-files with the GSP Editor (because of File Associations) even if the project is not a Grails Project.

So the solution is to convert the project in a Grails Project: right click on your project → “Configure” | “Convert to Grails-Project”.

Resources:

Versions: Eclipse 3.5, STS 2.3, Grails 1.1.1

Posted in Grails. Tags: , , . Leave a Comment »