0
votes

Can I bind default field if it doesn't satisfy constraints? Suppose I have

class MyCommand {
    String tag = "defaultTag"

    static constraints = {
        tag inList: ['a', 'b']
    }
}

When users passes ?tag=myHackieTag I don't wanna check is command object valid - just use default value (defaultTag)

2

2 Answers

1
votes

You could create your own set of getter setter methods for this, no?

class MyCommand {
    String tagValue

    void setTag( value ){
        tagValue = value in ['a', 'b' ] ? value : 'defaultTag'
    }

    String getTag(){
        tagValue
    } 
}

Not sure how this works with the new bindable stuff in grails 2.

0
votes

you can validate a single parameter, e.g.

if (!myCommand.validate(['tag'])) {
     // provide your default value when validation fails
     myCommand.tag = "defaultTag"
}