Nerds who love the symfony-project
5 Oct
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.
15 Responses for "Specify a range of years for sfWidgetFormDate"
Neat little tip! Thanks
[...] Specify a range of years for sfWidgetFormDate [...]
Hej ! Nice snippet !
Why don’t you propose a patch to the symfony code ? Such a feature would be nice
.
Here is the code of the widget :
http://trac.symfony-project.org/browser/branches/1.1/lib/widget/sfWidgetFormDate.class.php
Doesn’t seem to be so hard to change, maybe you could give it a try
. Well, for sure it’s always easier to say “do it” then to actually do it..
.
[...] klar, das konnte ich so nicht gebrauchen. Schnell war im Netz eine passende Lösung gefunden. Um die Auswahl und Anzeige von Jahren auch weiter in der Vergangenheit und der Zukunft zu [...]
This indeed should be fixed in Symfony. Thanks
[...] la idea no la saqué de mi cabeza, sino que la encontré buscando un poco en el blog symfonynerds en donde se desarrolla un poco más este [...]
Thks 4 Share bigm
$this->widgetSchema['birthdate'] = new sfWidgetFormDate(array(
‘years’ => array_combine(range(date(”Y”) - 105, date(”Y”) - 1), range(date(”Y”) - 105, date(”Y”) - 1)),
));
you must start a second blog, it’s great!
Okay it works, but isn´t it a very stupid way to configure a date field ?
shouldn´t there be something like
date from => 1900-01-01, date_to = date(”Y-m-d”) , that would be easier ….
where I nust add this code…
thanks
How did you define the date field in the schema.yml so it can have dates older than 1970? “bu_date”? If so, does it support ranges like the one in the example? (1900-2000)
Cheers
Simple and straight forward.
You saved me a half an hour
Thanks a lot.
[...] klar, das konnte ich so nicht gebrauchen. Schnell war im Netz eine passende Lösung gefunden. Um die Auswahl und Anzeige von Jahren auch weiter in der Vergangenheit und der Zukunft zu [...]
Leave a reply