Jesús Zazueta
Jesús Zazueta

Reputation: 1192

How can I specify optional request parameters for a Grails command object?

UPDATE: code changed to make it look Groovier XD

Like the title says. Suppose I have a command object:

@Validateable
class MyCommand {
    String myProperty1
    String myProperty2
    String myProperty3
    static constraints = {
        myProperty1(blank: false)
    }
}

And I also have a controller which tries to populate a new instance of my command object upon receiving a GET request:

class HeyController {
    def doSomething(MyCommand mc) {
        render [result: mc] as JSON
    }
}

Note that I only want to make myProperty1 a required parameter in this example (i.e. I'd like myProperty2 and myProperty3 to be optional request parameters). However, if I throw in this request:

http://myappserver:8080/app/hey/doSomething?myProperty1=foo

Grails will still complain that myProperty2 and myProperty3 have null values.

So, what am I doing wrong? Thanks!

Upvotes: 0

Views: 502

Answers (1)

Dónal
Dónal

Reputation: 187529

@Validateable
class MyCommandObject {
    String myProperty1
    String myProperty2
    String myProperty3


    static constraints = {

        myProperty2 nullable: true
        myProperty3 nullable: true
    }
}

Upvotes: 1

Related Questions