Incorrect options' values in the sfWidgetFormDate

转自: http://www.odino.org/281/incorrect-options-values-in-the-sfwidgetformdate

The cool stuff about sfWidgetForm[I18n]Date is that, obviously, you can specify which options' values should be included in the 3 selects rendering days, months and years.

For example, to specify custom years:

new sfWidgetFormDate(array('years' => range(2005, 2011));

and the select will contain values from 2005 to 2011.

The highly creepy thing of doing things this way is that the body of the option will be the value of your array, and the value of the option will be the key: without specifying any key, php will magically assign integer keys, starting from zero, to your array.

Something that gets translated into:

array{
'0' => '2005',
'1' => '2006',
...
}

and when you submit the form the years will drive you nuts.

The simplest approach to solve this stuff is to assign the array keys: 

$years_range = range(2005, 2011)
$years = array_combine($years_range, $years_range);
new sfWidgetFormI18nDate(array('years' => $years);
posted @ 2011-08-26 20:23  Lux.Y  阅读(390)  评论(0)    收藏  举报