开发者

Grails - how to hide a date inside a form

I have an 'edit' view for a model that has several fields (one of which is a date). I only want a few of the fields visible to allow edits, so I just hide the other fields using <g:hiddenField>

But one of the fields is of type TimeStamp and I can't seem to find a way to hide this in the form. I tried

<g:form method="post" >
    <g:textField name="firstName" value="${applicationUserInstance?.firstName}" />
  开发者_如何学编程  <g:textField name="lastName" value="${applicationUserInstance?.lastName}" />
    <g:datePicker name="createDate" style="visibility:hidden;" precision="day" value="${applicationUserInstance.createDate}" />

The date picker is still visible. Any idea how to hide the date so that I can just pass this to the update method upon submit of the form. Many thanks.


Just re-iterating Rob's comment here. No need to put that on the form. The only data you need on the form is the data you are updating and the ID of what is being updated. Everything else will just stay the same...

def update = {
    def applicationUserInstance = User.get(param.id)
    // at this point applicationUserInstance.createDate is
    // correct.
    applicationUserInstance.properties = params
    // since no createDate was in the params, it doesn't change.
    // so you're good
    applicationUserInstance.save(flush:true)
}


Actually my answer below might not be the right answer to your question. Otherwise if you really just have a createdDate-field, which should keep track, when the entry was created in the database, I suggest you do it the Grails-way and use the reserved keywords 'dateCreated' and 'lastUpdated'

Check http://grails.org/doc/1.3.7/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.5.1 Events and Auto Timestamping on how to use these. If you use these then my answer below will be helpful to control the visibility of these fields 'dateCreated', 'lastUpdated'

Suggestions for 'dateCreated', 'lastUpdated'

Probably you want this timestamp to be created automatically as you found it in the Grails documentation but you do not want it to be visible in your view.

Now, to exclude this timestamp from being visible, first grails install-templates

I assume you have grails-1.3.7

  • Go to src/templates/scaffolding and check your gsp-files, e.g. 'create' and 'edit'
  • Search for this line:

<% excludedProps = ["version", "id",

and edit for example 'dateCreated'

<% excludedProps = ["dateCreated", "version", "id",

There is also a tutorial on this topic http://www.ibm.com/developerworks/java/library/j-grails01209/index.html

Greetings,

Jan

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜