Returning Grails Validation Errors for Ajax Calls

January 6, 2011

I’m using Grails to create a REST service, and its built-in JSON formatting makes receiving and returning objects easy. When I get validation errors, I’d like to return the errors in JSON in a format that’s easy to use by my client. With a web client, Grails (and most other validation frameworks) by default outputs a list of human-friendly error messages. Ideally, what I’d like to return from my REST service is a list of strings, where each string is one field error.

Incidentally, REST’s HTTP status codes make it easy to distinguish when I’m returning objects vs. error messages. If I return a 200 or 201 status code, it’s success, and my client should expect an object from my data model back. If I return a 500 status code, my client should expect a list of error strings back.

It took me a little poking around in javadocs and Grails source to figure it out, but here’s the syntax that will work:


response.status = 500
render myGormObject.errors.allErrors.collect {
    message(error:it,encodeAs:'HTML')
} as JSON

Grails replaceAll With Captured Groups/Backreferences

October 13, 2010

In a Grails app, I found myself needing to do a regex search, save captured groups/backreferences (I’m not sure of the distinction between those terms), and replace them with new content, which includes those backreferences. But I was having trouble finding a way to do it.

I tried using $1 or \1, but whenever I did that, I got an error that it was an invalid captured group or backreference.

The solution I found was the following.


content = content.replaceAll( /(a*b)/,
{ full, word -> "${word}" } )

This takes every example of a*b in the string content (like ab, aaab, etc.), and puts an anchor tag around it.


Changing the Grails Default Server Port

March 9, 2010

The Grails tutorial I read said provided instructions for changing the default server port from 8080 to some other value, so that you would not need to specify an alternate server port every time you run-app. It said that you should open $GRAILS_HOME/scripts/Init.groovy and look for a line setting the value of serverPort.

The problem is that this tutorial was written in 2008, and while you wouldn’t think that much would have changed since then, apparently the Grails config files have been refactored. Init.groovy now appears to be a much higher-level file, and serverPort isn’t specified in it.

After searching the Grails directory, I found out that the new file to edit is $GRAILS_HOME/scripts/_GrailsSettings.groovy. Open it and look for the following line:

serverPort = getPropertyValue("server.port", 8080).toInteger()

I believe the getPropertyValue method checks properties sent in via the command line or configured in some other way. If it’s not set, though, it uses the second parameter as the default value. Change it from 8080 to some other port, like 9090, and the next time you run-app you’ll be running on the new port instead


List of All Grails Validation Constraints

February 22, 2010

For some reason, Grails decided to delete their published list of all validation constraints. This official-sounding page just sends you to the reference docs, and its validation page gives the unhelpful message that

A complete reference for the available constraints can be found on the reference guide

I’m not sure what other reference guide this could be referring to, not to mention what it would mean for this information to be found “on” the reference guide, as opposed to “in” it.

Update: @mr_paul_woods sent me to the Grails docs and told me to look in the sidebar. Turns out that I wasn’t supposed to click Validation on the right pane, but rather find “Constraints” in the unrelated left pane. There’s no way to get this list on a page by itself, so here they are to be helpful: