Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails Command Object Default Value

Tags:

grails

I am trying to use Grails Command Object to filter action parameters. But I want to set a default value if the parameter is not present in URL.

class ListCommand {

    String order = 'desc'
    String sort = 'startDate'

}

def list(ListCommand cmd) {
    println cmd.order
}

I thought the behaviour would be same as if I was creating a domain object. I don't want to handle each parameter in the action like:

cmd.order = params.order ?: 'desc'
like image 303
Dopele Avatar asked Feb 17 '26 10:02

Dopele


1 Answers

if you always use such action declarations:

def list(ListCommand cmd) { ... }

or

def list = {ListCommand cmd -> ...}

you may try this:

class ListCommand {
    String order
    String sort

    def beforeValidate() {
        order = order ?: 'desc'
        sort = sort ?: 'startDate'
    }
}

because in those action definitions validate() method always calls for command objects.

like image 182
jenk Avatar answered Feb 19 '26 02:02

jenk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!