Ask your Symfony questions! Pay money and get answers fast! (more info)

Why do the years not appear in the form date widgets? Symfony

  • SOLVED

Look at the screenshot. On the left you can see the created_at value in the database. On the right you can see it on the screen. The years do not appear. I'm having this problem with every date widget, where the date is from a source that I just imported.

Symfony 1.4.

Why are the years not showing up?

Answers (2)

2011-01-08

Gabor Fasi answers:

The year widget's options contain a few years back and forth from the current one by default. It's just guesswork without seeing the html source, but I believe 2005 is not included in the created_at wiget's options, thus selection the default empty option.

sfWidgetFormDate has an optional `years` option, where you can specify which years you want to have it displayed.

2011-01-10

Loban Rahman answers:

Check out the contents of configure() for sfWidgetFormDate:

$this->addOption('format', '%month%/%day%/%year%');
$this->addOption('days', parent::generateTwoCharsRange(1, 31));
$this->addOption('months', parent::generateTwoCharsRange(1, 12));
$years = range(date('Y') - 5, date('Y') + 5);
$this->addOption('years', array_combine($years, $years));

As you can see, by default it allows all days, all months, and the previous 5 to next 5 years. All 3 field ranges can be customised with the options. So pick a range that works for you. Or, you can make it extra smart by adding a bit of custom code that, regardless of the range of years given, will always include one option for the year that is in the model (by reading $this->getObject()), so that you never get a blank year.