Question

I need to generate a year drop down with cake to populate ArchiveQuarter.year in the database. This is currently set in the database to 'year', but it was producing the same results with 'date'.

This is the current setup:

echo $this->Form->input('ArchiveQuarter.year', array(
     'label'=>'Year',
     'type'=>'date',
     'dateFormat'=>'Y',
     'minYear'=>'2000',
     'maxYear'=>date('Y'),
));

The problem is that this produces a dropdown with a range of 1970-2013 instead of 2000-2013. The minYear does work though, because if you set it below 1970 then it will indeed work.

Was it helpful?

Solution 2

I did some research into what was going on within the FormHelper.php file and here is a breakdown:

  • In the dateTime() function it checks if $attributes['value'] is empty. If it is it fills that information using $this->value($attributes,$fieldName); populating it with 2013

  • 2013 is then parsed by _GetDateTimeValue()

  • Within _GetDateTimeValue() it is parsed as numeric, and is translated from unix time; an hour and a half into 1970.

  • That then is set as the value of the final select box, which has to expand from the minYear to properly accommodate.

For whatever reason Cake does not take into account the explicitely stated dateFormat=>'Y' when is parses with _GetDateTimeValue().

The solution is to insert: $selected => strtotime('now') which will in the end get properly parsed.

You can also explicitly set $selected to values outside of the range of dates, which I'm not sure would be a wanted before or not.

OTHER TIPS

I literally copied and pasted your code into my CakePHP 2.3.0 app, and it worked as expected - gave me a select input from 2013 to 2000.

Maybe try getting the most recent version of CakePHP (your question doesn't list what version you're using) and try that?

Bottom line, the code you've provided does work - so if it's not for you, there are other factors in play.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top