Command Object data binding broken in Grails 2.1.x?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Command Object data binding broken in Grails 2.1.x?

Marcel Overdijk
Regarding my previous post related to date binding I'm experiencing now a much broader issue regarding data binding using Command Objects in 2.1.1

Take the following form:

<g:form method="post">
    <g:textField name="name" />
    <g:field name="age" type="number" />
    <g:datePicker name="date" precision="day" value="${cmd.date}" />
    <button type="submit">Submit</button>
</g:form>

And it's corresponding controller / command object:

class MyController {

    def index(MyCommand cmd) {
        println "params = ${params}"
        println "cmd = ${cmd}"
        println "cmd.name = ${cmd.name}"
        println "cmd.age = ${cmd.age}"
        println "cmd.date = ${cmd.date}"  
    }
}

class MyCommand {
        Date date
        String name
        Integer age = 0
}


When submitting the form with values only the String property gets binded. The Integer and Date fields are not binded, although values are in params object.

The output from the action looks like:

params = [date_day:8, name:saddsfdsaf, age:4, date_year:2012, date_month:10, date:Mon Oct 08 00:00:00 CEST 2012, action:index, controller:my]
cmd = org.mycompany.MyCommand@53635ac1
cmd.name = saddsfdsaf
cmd.age = 0
cmd.date = null

As you can see age and date are not binded, but were in params map.

Reply | Threaded
Open this post in threaded view
|

Re: Command Object data binding broken in Grails 2.1.x?

Marcel Overdijk
Going further...

I tried the bindData method manually by adding the following to the controller:

def target = new MyCommand()
bindData(target, params)
println "target = ${target}"
println "target.name = ${target.name}"
println "target.age = ${target.age}"
println "target.date = ${target.date}"

and also in this case only String property is binded, and not the Integer and Date.

params = [date_day:8, name:saddsfdsaf, age:4, date_year:2012, date_month:10, date:Mon Oct 08 00:00:00 CEST 2012, action:day, controller:revenueReport]
target = org.mycompany.MyCommand@3b845321
target.name = saddsfdsaf
target.age = 0
target.date = null


Also note that println "params.age = ${params.age}" prints out 4 as expected.

Btw I don't see other issues regarding data binding in the application. So data binding from controller to domain classes work as expected. Also with Integers and Dates.





params.age = 4
               
                println "params.age = ${params.age}"
Reply | Threaded
Open this post in threaded view
|

Re: Command Object data binding broken in Grails 2.1.x?

Jeff Brown-4
In reply to this post by Marcel Overdijk

On Oct 8, 2012, at 4:18 AM, Marcel Overdijk wrote:

> Regarding my previous post related to date binding I'm experiencing now a
> much broader issue regarding data binding using Command Objects in 2.1.1
>
> Take the following form:
>
> <g:form method="post">
>    <g:textField name="name" />
>    <g:field name="age" type="number" />
>    <g:datePicker name="date" precision="day" value="${cmd.date}" />
>    <button type="submit">Submit</button>
> </g:form>
>
> And it's corresponding controller / command object:
>
> class MyController {
>
>    def index(MyCommand cmd) {
>        println "params = ${params}"
>        println "cmd = ${cmd}"
>        println "cmd.name = ${cmd.name}"
>        println "cmd.age = ${cmd.age}"
>        println "cmd.date = ${cmd.date}"  
>    }
> }
>
> class MyCommand {
> Date date
> String name
> Integer age = 0
> }
>
>
> When submitting the form with values only the String property gets binded.
> The Integer and Date fields are not binded, although values are in params
> object.
>
> The output from the action looks like:
>
> params = [date_day:8, name:saddsfdsaf, age:4, date_year:2012, date_month:10,
> date:Mon Oct 08 00:00:00 CEST 2012, action:index, controller:my]
> cmd = org.mycompany.MyCommand@53635ac1
> cmd.name = saddsfdsaf
> cmd.age = 0
> cmd.date = null
>
> As you can see age and date are not binded, but were in params map.
>



Please file a JIRA and attach a sample app.

Thanks for the help!



JSB
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Command Object data binding broken in Grails 2.1.x?

Marcel Overdijk
I created a simple test-app and there the problem does not occur. So no worries for now.

I will investigate further what is going on and come back here.
Reply | Threaded
Open this post in threaded view
|

Re: Command Object data binding broken in Grails 2.1.x?

Marcel Overdijk
In reply to this post by Jeff Brown-4
Found the issue.

The form submit was going through a different action (default one)

def index() {
    redirect(action: "actualAction", params: params)
}

def actualAction(MyCommand cmd) { ...


So basically all params came through but off course no binding to MyCommand.


Reply | Threaded
Open this post in threaded view
|

Re: Command Object data binding broken in Grails 2.1.x?

Jeff Scott Brown

On Oct 8, 2012, at 2:17 PM, Marcel Overdijk wrote:

> Found the issue.
>
> The form submit was going through a different action (default one)
>
> def index() {
>    redirect(action: "actualAction", params: params)
> }
>
> def actualAction(MyCommand cmd) { ...
>
>
> So basically all params came through but off course no binding to MyCommand.
>
>

It still isn't clear to me why only the String parameters are being bound.  If that is happening, still sounds like a bug even if you are doing a redirect which leads to the action in question.



JSB

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email