I am wanting to do a date of birth composite drop-down for date of birth (i.e. three drop-downs - one for day, one for month, one for year). Helpfully, ZF2 comes with this element built in, called DateSelect
Here is how I am adding it to the form:
$this->add([
'name' => 'date_of_birth',
'type' => 'DateSelect',
'attributes' => [
'required' => true,
'value' => date('Y-m-d', strtotime('18 years ago')),
],
'options' => [
'label' => 'Date of Birth',
'max_year' => date('Y', strtotime('18 years ago')),
],
]);
To render the form I am doing:
echo $this->form($form);
Anyway, the day element is being rendered in j format (i.e. day of the month without leading zeros 1 to 31).
How do I tell the element to render the day in d format (i.e. day of the month, 2 digits with leading zeros 01 to 31)?
The view helper
Zend\Form\View\Helper\FormDateSelecthas a functiongetPatternthat generates the patten by using theIntlDateFormatterclass:There did not appear to be any method of setting a pattern at element level, so I have extended this view helper allowing me to specify my own pattern:
Very basically, this takes the returned pattern array (
['day'=>'d', 'month' => 'MMMM', 'year' => 'yyyy']), checks if the value ofdayis a single letter "d" and, if so, replaces it with a double-d.