Grails

Empty date field cannot be persisted using view

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Fixed
  • Affects Version/s: 1.0-RC1
  • Fix Version/s: 1.1-beta3
  • Component/s: Persistence, TagLib
  • Labels:
    None

Description

As described on http://www.nabble.com/Date-not-valid%3A-date-can-be-null--tf4759422.html empty date fields cannot be persisted, resulting in an invalid date error message.

Using noSelection attribute as in

<g:datePicker name="myDate" value="${new Date()}" noSelection="['':'-Choose-']"/>

does not work.

Issue Links

Activity

Hide
Marc Guillemot added a comment -

A workaround is:

1) use g:datePicker undocumented default="..." attribute in the view

<g:datePicker name="myDate" value="${new Date()}" noSelection="['':'-Choose-']" default="none"/>

2) remove the field from the map used for binding if some/all fields is/are empty

def excludes = []
if (!params.myDate_day)
excludes << "myDate"
bindData(myDomainObject, params, excludes)

(note that this has to be done in both save and update and that bindData has to be used rather than properties assignment)

Show
Marc Guillemot added a comment - A workaround is: 1) use g:datePicker undocumented default="..." attribute in the view <g:datePicker name="myDate" value="${new Date()}" noSelection="['':'-Choose-']" default="none"/> 2) remove the field from the map used for binding if some/all fields is/are empty def excludes = [] if (!params.myDate_day) excludes << "myDate" bindData(myDomainObject, params, excludes) (note that this has to be done in both save and update and that bindData has to be used rather than properties assignment)
Hide
dion gillard added a comment -

I'm on Grails 1.0.1 and workaround 1) doesn't work for me.

I have:

<g:datePicker precision="day" name="publicLiabilityExpiry" value="${contractor?.publicLiabilityExpiry}" default="none" noSelection="${['':'--']}"/>

and need to use Option 2 to get it to work.

Show
dion gillard added a comment - I'm on Grails 1.0.1 and workaround 1) doesn't work for me. I have: <g:datePicker precision="day" name="publicLiabilityExpiry" value="${contractor?.publicLiabilityExpiry}" default="none" noSelection="${['':'--']}"/> and need to use Option 2 to get it to work.
Hide
Marc Guillemot added a comment -

Seems that I wasn't precise enough: (1) and (2) are not different alternatives but the 2 steps of the workaround

Show
Marc Guillemot added a comment - Seems that I wasn't precise enough: (1) and (2) are not different alternatives but the 2 steps of the workaround
Hide
Dennis Carroll added a comment -

I had to use the aforementioned workaround for the save operation:

def hmaCellInstance = new HmaCell()
def excludes = []
if ((!params.demolishedDate_month) ||(!params.demolishedDate_day)||(!params.demolishedDate_year))
excludes << "demolishedDate"
bindData(hmaCellInstance, params, excludes)
// def hmaCellInstance = new HmaCell(params)
...

but was able to make a more generic one for the update operation:

def update = {
def hmaCellInstance = HmaCell.get( params.id )

if(hmaCellInstance) {
// Workaround for http://jira.codehaus.org/browse/GRAILS-1793
def dateError = paramsDateCheck(params)
hmaCellInstance.properties = params
if(!hmaCellInstance.hasErrors() && hmaCellInstance.save()) {
def msg = "Cell ${params.id} updated${dateError}."
...

where
def paramsDateCheck ( params ) {
def rc
def names = []
params.each{ if (it.value == "struct") names.add(it.key) }
names.each {
def aDateString = ""
def allBlank = (params[it+"_day"] == '' & params[it+"_month"] == '' & params[it+"_year"] == '')
def someBlank = (params[it+"_day"] == '' | params[it+"_month"] == '' | params[it+"_year"] == '')
if (allBlank)
params[it] = null
if (someBlank & !allBlank){
params[it] = null
def day = params[it+"_day"] == ''?'dd':params[it+"_day"]
def mon = params[it+"_month"] == ''?'mm':params[it+"_month"]
def year = params[it+"_year"] == ''?'yyyy':params[it+"_year"]
aDateString = "${day}/${mon}/${year}"
rc = ", ${it}, ${aDateString}, ignored"
}
}
return rc?rc:""
}

Show
Dennis Carroll added a comment - I had to use the aforementioned workaround for the save operation: def hmaCellInstance = new HmaCell() def excludes = [] if ((!params.demolishedDate_month) ||(!params.demolishedDate_day)||(!params.demolishedDate_year)) excludes << "demolishedDate" bindData(hmaCellInstance, params, excludes) // def hmaCellInstance = new HmaCell(params) ... but was able to make a more generic one for the update operation: def update = { def hmaCellInstance = HmaCell.get( params.id ) if(hmaCellInstance) { // Workaround for http://jira.codehaus.org/browse/GRAILS-1793 def dateError = paramsDateCheck(params) hmaCellInstance.properties = params if(!hmaCellInstance.hasErrors() && hmaCellInstance.save()) { def msg = "Cell ${params.id} updated${dateError}." ... where def paramsDateCheck ( params ) { def rc def names = [] params.each{ if (it.value == "struct") names.add(it.key) } names.each { def aDateString = "" def allBlank = (params[it+"_day"] == '' & params[it+"_month"] == '' & params[it+"_year"] == '') def someBlank = (params[it+"_day"] == '' | params[it+"_month"] == '' | params[it+"_year"] == '') if (allBlank) params[it] = null if (someBlank & !allBlank){ params[it] = null def day = params[it+"_day"] == ''?'dd':params[it+"_day"] def mon = params[it+"_month"] == ''?'mm':params[it+"_month"] def year = params[it+"_year"] == ''?'yyyy':params[it+"_year"] aDateString = "${day}/${mon}/${year}" rc = ", ${it}, ${aDateString}, ignored" } } return rc?rc:"" }
Hide
michael added a comment -

this bug is present in my grails version ( 1.1.1 )
Workaround is ok but this bug will be corrected in futur release ? I must create a new bug report ?

Show
michael added a comment - this bug is present in my grails version ( 1.1.1 ) Workaround is ok but this bug will be corrected in futur release ? I must create a new bug report ?
Hide
Marcel Overdijk added a comment -

@micheal: seems like a regression. I would indeed reopen or create a new issue (make sure to set affects version correctly).
Note that in Grails 1.2 M1 there is also a problem with empty dates (GRAILS-4922).

Show
Marcel Overdijk added a comment - @micheal: seems like a regression. I would indeed reopen or create a new issue (make sure to set affects version correctly). Note that in Grails 1.2 M1 there is also a problem with empty dates (GRAILS-4922).
Hide
Dennis Carroll added a comment -

This remains a bug in 1.2M3 and if I had a clue where to fix it I would.
Thanks for playing.

Show
Dennis Carroll added a comment - This remains a bug in 1.2M3 and if I had a clue where to fix it I would. Thanks for playing.
Hide
Graeme Rocher added a comment -

Bulk closing bunch of resolved issues

Show
Graeme Rocher added a comment - Bulk closing bunch of resolved issues

People

Vote (12)
Watch (16)

Dates

  • Created:
    Updated:
    Resolved: