0

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 Answers 2

1

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.

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

Comments

0

you can validate a single parameter, e.g.

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

1 Comment

Thanks for solution but it will require a lot of additional boilerplate code. Can I make this behaviour by default?

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.