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?
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 manualreject()calls to be included in the errors, put them after the call tovalidate().