Reputation: 207
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.
class IncomeTransaction {
Date incomeDate
------
}
<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
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
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
Reputation: 53793
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
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