Command Objects are a great way to handle this.
At the bottom of your controller add a command object. Then you can use this command object in your controller. (Unverified, but it seemed if the command object was at the top of the controller the hot reloading stopped working)
class MyController {
def save = {MyCommand ->
// This will validate the command object based on its constraints
cmd.hasErrors()
}
}
class MyCommand {
Boolean switch
String value
static constraints = {
switch(nullable:false)
value(nullable:false)
}
}
And of course the command object has all the constraints available to it. So if you wanted value to be mandatory only if switch it true, do something like this
class MyCommand {
Boolean switch
String value
static constraints = {
switch(nullable:false)
value(validator: {val, obj->
if (obj.properties['switch'])
val ? true : ['default.null.message']
})
}
}
No comments:
Post a Comment