1

Having the following commandobjects in Grails:

class commandA implements commandObjectType {
    String a

    static constraints = {
        a blank: false
    }
}

and

class commandB extends commandA {
    String b
}

How would one the implement custom field validation on b in the commandB object? It is to my knowledge not possible to override or in other ways change a closure..

Can this be done in anyway? I have tried "shifting" in a closure, without succes.. Is it possible to specify validators in-line with the fields in any way?

2 Answers 2

1

I'm not sure if sharing contraints works for command objects, but you can try something like this:

class commandB extends commandA {
    String b

    static constraints = {
        importFrom commandA
    }
}

See the Grails documentation about constraints usage.

Sign up to request clarification or add additional context in comments.

1 Comment

It might work in a situation, where there is no inheritance involved.
0

Just define "constraints" block in commandB as usual with validation rules for "b" field. Validation should work for both "a" and "b" fields.

3 Comments

No, that is not possible. Also, if you think about it in a minute - you cant have to closures of the same name. It throws MissingMethodException..
@Hoof you can have static properties of the same name in both the parent and the child class, there's no concept of one "overriding" the other when they're static.
@Ian, OK, I did not know that. However, it doesn't change the fact, that it doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.