0

This is probably a ridiculously easy question, but I've been struggling with it nonetheless.

I have a form:

.
.
.
<g:form controller='somecontroller' action='someaction'>
    <g:textField name='name'/>
    <g:checkBox name='active'/>
    <g:submitButton name='save'/>
</g:form>
.
.
.

And I have a command object:

class MyCommandObject{

    String name
    Boolean active

    static constraints{
        .
        .
        .
    }
}

When I try to access the active property of the command object in my controller, it's always false. The name property, however, is being properly populated. I've tried changing the value to true, and also ${true} (as I saw in some example) and none seem to work. What am I doing wrong here?

Edit: How I'm binding to the command object in the controller:

class MyController{

    .
    .
    .

    def save(MyCommandObject cmd){

        // Do validation/error checking

        def myDomainObject = new DomainObject()

        myDomainObject.name = cmd.name
        myDomainObject.active = cmd.active

        // Do something with myDomainObject

    }

}

I've also tried the style:

def save = { MyCommandObject cmd ->

    .
    .
    .
}
aasukisuki
  • 1,186
  • 1
  • 11
  • 33
  • How are you binding to the command object in the controller? – Kelly Jul 18 '12 at 21:36
  • You are right - this should be and usually is simple. I've just verified both of these methods on Grails 2.0.4 and the second method on 1.3.7. Both print `true` or `false` correctly when I `println cmd.active` doing exactly what you are. Have you tried a `grails clean` - not sure what else to try. Everything you've done looks fine as long as your actual form has the correct controller and action and no typo's your demo form above doesn't show. – Kelly Jul 18 '12 at 22:53
  • @Kelly thanks for investigating. The strange thing is that all of the other fields work, just not checkbox / boolean fields. – aasukisuki Jul 19 '12 at 00:11
  • Follow-up: I did a grails clean as suggested by @Kelly and that corrected the issue. If you want to answer the question with that, I'll accept it. – aasukisuki Jul 19 '12 at 20:35
  • Thanks! Just posted as an answer. – Kelly Jul 19 '12 at 22:28

1 Answers1

1

Everything you've done looks fine and I've verified as working in 1.3.7 and 2.0.4.

Please do a grails clean and try again.

Kelly
  • 3,709
  • 4
  • 20
  • 31
  • 2
    I encountered the same thing in Grails 2.1.1. Binding a parameter to a Boolean member in a command object always resulted the member to be null. After grails clean (and project clean in GGTS) everything was ok. Maybe there was too much sunspots? – apa64 Nov 01 '12 at 18:41
  • It seems like command objects are still a little half-baked, even in 2.2.0. Maybe 3/4-baked ;) – Charles Wood Oct 13 '14 at 22:01
  • Yeah grails clean is required in 2.2.4 as well – Sudhir N Oct 23 '15 at 06:29