Search code examples
grailsjodatimegsp

Grails, Joda-Time plugin, TimePicker nullable not working


I have a domain class with the attributes start + end, both of them are LocalTime (Joda-Time plugin). I want the end Time to be nullable. For this purpose I used the following constraint: end nullable: true. Scaffolding is realized through the Joda template and it's working pretty good. The only problem is that selecting nothing with the timepicker for the end property results in: Cant populate a class org.joda.time.LocalTime without a hour.

This is the generated code in the view:

<div class="fieldcontain ${hasErrors(bean: dailyBookingInstance, field: 'end', 'error')} ">
    <label for="end">
        <g:message code="dailyBooking.end.label" default="End" />
    </label>
    <joda:timePicker name="end" value="${dailyBookingInstance?.end}" default="none" noSelection="['': '']"></joda:timePicker>
</div>

Solution

  • This is a binding error - Joda Time Plugin - Fields nullable?

    One work around is--

    Change field name in the view, say time

    <joda:timePicker name="time" value="${myDomainInstance?.end}" default="none" noSelection="['': '']"/>
    

    and only populate this in your domain instance if data exists, like this

    def save(MyDomain myDomainInstance) {
    
      if(params.time_minute && params.time_hour){
        myDomainInstance.end = new LocalTime(params.int('time_hour'), params.int('time_minute'))
        myDomainInstance.validate()
      }
    
      //Other save code
    }
    

    Hope this helps..,.