Ashish
Ashish

Reputation: 207

Property must be a valid Date in grails

I have used incomeDate as Date and trying to submit as string '2015-08-02'. And I have trying to submit it different way but getting error.

Domain

class IncomeTransaction {
    Date incomeDate
    ------
}

.gsp

<input type="text" name="incomeDate" placeholder="yyyy-mm-dd" />

Controller:

String dateString = params.incomeDate
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse(dateString);
incomeTransactionInstance.incomeDate = myDate

// get error like ---- Property incomeDate be a valid Date in grails

/*Another way*/
//params.incomeDate = new Date().parse( 'yyyy-MM-dd', params.incomeDate )
// get error like ---- unparsable 

Upvotes: 1

Views: 1081

Answers (3)

Ibrahim.H
Ibrahim.H

Reputation: 1195

A global setting can be configured in grails-app/conf/application.groovy (for Grails 3, and at grails-app/conf/Config.groovy for Grails 2) to define date formats which will be used in the application wide when binding to Date, like so:

grails.databinding.dateFormats = ['yyyy-MM-dd']

Hope this helps.

Upvotes: 0

zoran119
zoran119

Reputation: 11327

An alternative to using <g:datePicker /> you can also use a regular input like you have in your gsp page. Just add the following to your domain class:

import org.grails.databinding.BindingFormat

class IncomeTransaction {
    @BindingFormat('yyyy-MM-dd')
    Date incomeDate
}

This is useful when you use a date picker (jQuery Datepicker for example).

Upvotes: 4

Frederic Henri
Frederic Henri

Reputation: 53793

  1. There is a good thing with grails if you dont know but want to find out by yourself, you can always generate controller and view by grails and look how it is done

  2. what I am using for date is not an input type text but the following :

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

in your controller you do not need any parsing then :

incomeTransactionInstance.properties = params

and save your object

Upvotes: 4

Related Questions