开发者

Grails validate() overwrites rejects

I'm aware that i开发者_开发技巧t'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 manual reject() calls to be included in the errors, put them after the call to validate().


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

Grails read() Docs

The resulting relationship between manually adding errors and grails automatic behavior is rather non-intuitive in my opinion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜