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?

Hoof
  • 1,738
  • 2
  • 17
  • 39

2 Answers2

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.

Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
0

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

Sergei Shushkevich
  • 1,356
  • 10
  • 14
  • 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 Dec 19 '12 at 14:26
  • @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 Roberts Dec 19 '12 at 16:02
  • @Ian, OK, I did not know that. However, it doesn't change the fact, that it **doesn't work**. – Hoof Dec 20 '12 at 07:38