views:

29

answers:

1

Does anybody know how I could get the fieldError to print out in the example below.

for each item with an error, I would like to print custom error messages that I have defined in the messages.properties file

at the moment all this does is print the default error codes

item.errors?.allErrors?.each{ 
println it.toString() 
};

I have seen other examples where you can lookup an error code for a field e.g.

it.getFieldError('title').code

but I would like to convert the default message into my new error message and print that

thanks for your help

+1  A: 

You need access to the messageSource bean, e.g. with

def messageSource

in your controller or service. Then you can access the messages with

def locale = Locale.getDefault()
for (fieldErrors in bean.errors) {
   for (error in fieldErrors.allErrors) {
      String message = messageSource.getMessage(error, locale)
   }
}
Burt Beckwith
put def messageSource (in controller or service)Thanks this worked.item.errors?.allErrors?.each{ println messageSource.getMessage(it, null)};I also found a good link which explains this betterhttp://johnrellis.blogspot.com/2010/02/retrieve-grails-domain-errors-from.html
MTH