1

In controller's action Date argument used

def action(Date start, Long id){
   ....
}

Data binding by default not works for Date type. During invocation start variable initialized by current time for parameter value '01/05/2016'.

Date binding works well for domains with Date start field

someDomain.properties = params
bindData(someDomain, params)

How to activate it for action arguments?

PS

grails.databinding.dateFormats configured well

Grails 2.4.5

Updated:

I have seen Binding a Grails date from params in a controller , I use provided solution with ValueConverter for custom binding, and it works only for domains and commands binding

Community
  • 1
  • 1
demon101
  • 544
  • 1
  • 11
  • 39
  • Did you try to use command object instead of these two arguments? – Michal_Szulc Aug 05 '16 at 09:38
  • @Michal_Szulc with command object it should work, but it not suitable for me – demon101 Aug 05 '16 at 09:41
  • Possible duplicate of [Binding a Grails date from params in a controller](http://stackoverflow.com/questions/2871977/binding-a-grails-date-from-params-in-a-controller) – Vinay Prajapati Aug 05 '16 at 12:48
  • @VinayPrajapati no. The question was not about action arguments. Also, I have tested solutions with ValueConverter before writing the question – demon101 Aug 05 '16 at 13:14

1 Answers1

1

If you want dates to be bound using the formats configured under grails.databinding.dateFormats then you either need to use a command object or call one of the overloaded methods params.date('start').

I don't know why you're so reluctant to use a command object. You've said in a comment that this solution is "not suitable", but I'm curious to know why a command object is unsuitable.

Binding dates directly to action arguments is a feature that simply does not exist.

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • `Binding dates directly to action arguments is a feature that simply does not exist.` which is interesting, because it is possible in Spring. Maybe something possible in Grails 3.x? – Gregg Aug 05 '16 at 19:07
  • @Gregg yes. it's possible in spring MVC. And I don't want to produce dummy command object only for date binding – demon101 Aug 05 '16 at 21:25
  • @Dónal params.date('start') is require for date format in message properties for every variable name. Try to execute params.date('start') and will get exception – demon101 Aug 05 '16 at 21:27
  • @demon101 there are overloaded versions of `params.date()` that allow you to supply one or more date formats to use – Dónal Aug 06 '16 at 11:13
  • @Dónal for date format Grails can use default format from message.properties. And I prefer also use some default format (in the best case with localization support). But construction like params.date('my_date', message(code:'default.date.format')) or params.date('my_date', MySettings.DATE_FORMAT) looks ugly – demon101 Aug 08 '16 at 07:54