So work is beginning in the symfonynerds camp on a few 1.1 apps (just in time to have to migrate to 1.2 :p) and I thought it was time to start documenting some of the little tips we have for it.

Today I was fine tuning an app for UAT and wanted to configure the ‘years’ being listed in a sfWidgetFormDate Date Select.

In the first instance, it was for a ‘date of birth’ field, so I was looking for a range from about 1900 to 2000. The first thing that came to mind was the php function range() but as per this ticket raised (which I commented on as well as posting here) doesn’t work because the array keys increment from 0… So if you selected 1900, your year would be submitted as 0.

One way around it is to create a variable of your own with keys and values for the years, then pass it as the ‘year’ option, like the below.

$years = range(1920, 2000); //Creates array of years between 1920-2000
$years_list = array_combine($years, $years); //Creates new array where key and value are both values from $years list
 
new sfWidgetFormDate(array('years' => $years_list))

Perhaps in the future Symfony could offer a way of automating this with ‘year_min’ and ‘year_max’ options for sfWidgetDormDate, or readers have a faster way of doing it.