0

I have a domain object with size validation on two fields. The object is basically something like:

class foo {
    String name
    String description

    static constraints = {
        name nullable: false, blank: false, size: 3..31, validator { val, obj ->
            // additional data validation returning a code if it fails
        }
        description nullable: true, size: 3..255
    }
}

The problem is that I expect the "default" error to be of the form class.field.error (com.Foo.description.size.toosmall, for example) but in reality, when the domain class generates the list of codes, the final code is "size.toosmall", not the fully qualified version. So getCode is not very useful.

How is the list of codes generated and what is the best way to resolve this issue? I thought about iterating all the codes and using messageSource to look them up but this seems pretty crazy and extreme. And what baffles me is that I've never seen this issue before... making me think that somehow, something different is happening to cause the strange order of the codes. Or am I completely misunderstanding how it should work?

billjamesdev
  • 14,554
  • 6
  • 53
  • 76
Tuishimi
  • 171
  • 8
  • Why you would think the very specific error code would be the last one checked, since it's going to stop once it finds one that has a translation in message.properties? – billjamesdev Mar 10 '15 at 00:59
  • The code iterated through allErrors instead of fieldErrors. I inherited it and couldn't figure out why it wasn't (when the error was passed to messageSource) figuring out the correct code. It turns out the code was just wrong. Instead of letting gorm do its thing, we were manually doing it, by changing to fieldError, it was able to walk the error codes and pick the correct one. – Tuishimi Mar 11 '15 at 01:32

1 Answers1

1

Alright... figured out that instead of iterating over allErrors and trying to extract the codes THAT way, you can instead iterate over fieldErrors and pass the entire fieldError into messageSource and it will self resolve.

So...

domainObject.errors.fieldErrors.each {
    someList << messageSource.getMessage(it, locale)
}

Hope this helps someone else in the future.

Tuishimi
  • 171
  • 8
  • 1
    This is the preferred method. See [this answer](http://stackoverflow.com/a/3265997/2446208) also. – blacktide Mar 09 '15 at 21:30
  • Well, if someList has other stuff going into it, anyway, otherwise you could use "collect" instead of each, like someList = domainObject.errors.fieldErrors.collect { messageSource.getMessage(it, locale) } – billjamesdev Mar 10 '15 at 01:00
  • I assume you're using these strings in a non-GSP-page somewhere? Because grails has taglibs for displaying resolved fieldErrors (http://grails.github.io/grails-doc/2.4.x/ref/Tags/hasErrors.html). You're making information about those errors harder to access by just converting them to strings (such as the fieldname that had the error, etc). – billjamesdev Mar 10 '15 at 01:04
  • Hi Bill, thank you! You are definitely right about using collect {}, I should do that... the messages are being converted to JSON in response to an ajax call... – Tuishimi Mar 10 '15 at 05:24