• Feeds

    Get updates delivered to your reader or your email in-box: Subscribe to RSS or .

  • About Me

    I am a Java, Groovy and Grails consultant and software architect. I have worked with Java for many years, but recently I have taken an interest in new languages for the Java Platform - such as Groovy!   read more

  • Find Me

Grails Tip: Using failOnError When Saving Domain Instances

Calling the save method on a Grails domain class will return a null if there was an error in validation. This is handy when doing something like the following.

if( !book.save() ) {
   book.errors.each {
        println it
   }
}

In Grails 1.2 a feature was added to allow the option of forcing save to throw a grails.validation.ValidationException by using the failOnError argument.

try {
     book.save(failOnError:true)
} catch(ValidationException e) {
   // handle
}

If you prefer to make failing on validation errors the default, you can update Config.groovy in your Grails project to set the following property.

grails.gorm.failOnError=true

Thanks to Jeff Brown for reminding me of this when replying to a recent thread on the Grails user email list.

Related Posts with Thumbnails
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Printed from: http://www.brentbaxter.com/2010/03/10/grails-tip-using-failonerror-when-saving-domain-instances/ .
© Brent Baxter 2010.

Trackbacks/Pingbacks

  1.   Usando ‘failOnError’ al guardar instancias del dominio (inglés) - Observatorio de Grails
  2. GORM: default behaviour is not to throw Exception when save() fails | RETHROW EXCEPTION

RSS feed for comments on this post , TrackBack URI

Leave a Comment