Grails binds object data binding to id field

When using command objects, it seems like I don't get the id field auto-binding

class somethingCommand { int id String A String B // some methods here like Domain.get(id) } 

My lines A and B get auto-magical data bound to form properties, but not id. Other grails "hidden fields", such as version, dateCreated, or lastUpdated, are also bound correctly.

My current fixed solution: I resort to defining another hidden id field in my form

 <g:hiddenField name="blogId" value="${blog?.id}"/> 

And rename id to blogId in obect command and it works.

This is not like Grails elegance. What am I missing in the data binding rules for a Command vs controller object?

+7
source share
2 answers

Following this issue:

I ran into the same problem: I had a command with the id parameter. When my controller was called for an action that used this command, all parameters were correctly bound except for id .

It turned out that if your team has a field named version , the id field will not be assigned.

If you change the name of your version field for something else (i.e. readVersion), then the identifier will be displayed correctly.

Hope this helps,

Vincent Giger

+7
source

I have used this several times.

The id parameter is bound to your command like any other field. There is no special behavior in this particular field.

Now, if you send a value for the id field, which is incompatible with the type of the identifier field of your team, then the field will not be associated. You will not get a ClassCastException or anything like that. You just get a null value for the field.

I remember that I saw something complicated: if you have id in your URL (for example, controller / action / id) and in your form, the id from the URL takes precedence.

So, if your form has the correct hidden field for ID

 <field type="hidden" name="id" value="1"/> 

but the action is somehow superimposed on your form

 <g:form action="doSometing" id="some-incompatible-value">...</g:form> 

What would you get in the controller:

 params.id = "some-incompatible-value" 

This would make it impossible to convert Grails to your id parameter long or int, and your command object would have

 command.id = null 

So, I would advise: start over and rewrite your form from scratch and make sure that the value in your form from your controller params.id is compatible with the type of field of your command.

Let me know how this happens.

Vincent Giger

+3
source

All Articles