Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails validate() overwrites rejects

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?


1 Answers

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().

like image 114
Burt Beckwith Avatar answered Jan 02 '26 01:01

Burt Beckwith