Reputation:
I'm aware that it's a bug, but calling validate() on a domain class overwrites any rejects that are put in before:
def save = {
def assignment = new Assignment(params)
assignment.errors.reject("assignment.error")
// Save
if (assignment.validate()) {
//rejected error is gone
assignment.save()
redirect action: "list"
}
else {
//render errors
render view: "create", model: [instance: assignment]
}
}
So, until this issue is fixed (it's been around since grails 1.0 and it's almost 2.0 now), is there any smart workaround to preserve rejects and use the if validate() then save() paradigm at once?
Upvotes: 3
Views: 722
Reputation: 3488
@Burt is right, sadly. It's by design, though that design is sketchy. The problem is that grails validates on its own behind the scenes in certain cases, wiping custom errors where they should not be wiped.
So not only do you have to avoid calling validate(), you have to avoid the platform silently erasing your errors at various points as well.
Sometimes you can get around this by using Domain.read(params.id) instead of Domain.get(params.id).
The resulting relationship between manually adding errors and grails automatic behavior is rather non-intuitive in my opinion.
Upvotes: 0
Reputation: 75671
It's not a bug, it's by design. By calling validate()
you're asking for the validation process to start over again. If you want manual reject()
calls to be included in the errors, put them after the call to validate()
.
Upvotes: 3