5
votes

I'm using Sonata Admin Bundle with Symfony2. I have a datetime field. This is my piece of code. Pretty simple.

->add('passportIssueDate', 'date')

But in the form I can select Year value only from 2008 to 2018. Is there a way to set different range? Thanks in advance.

1

1 Answers

8
votes

You would use something like this:

$builder->add('dob', 'birthday', array('years' => range(1980, date('Y')), 'format' => 'dd-MMMM-yyyy'));

This means the Date Field (regardless of if it's DateTime, Date, or Birthday) will give you a range from 1980 in this case to the current year. To get a range including years in the future, you would use something like this:

$builder->add('startDate', 'date', array('years' => range(2012, 2020), 'format' => 'dd-MMMM-yyyy'));

This instance gives you a range from 2012 to 2010.

Furthermore, the 'format' => 'dd-MMMM-yyyy' will format your date selection.