Reputation: 171
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?
Upvotes: 0
Views: 119
Reputation: 171
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.
Upvotes: 1