Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 DateSelect/MonthSelect formatting

With the newly released version of Zend Framework 2, two new form elements were added; DateSelect and MonthSelect. I want to use the former, which adds three selects; day, month and year. However, I have a problem formatting the output how I want - and, there is no documentation for this!

I am adding the form element to my form like this (taken from this page):

$this->add(array(
    'type'    => 'Zend\Form\Element\DateSelect',
    'name'    => 'birthDate',
    'options' => array(
        'label'               => 'Date',
        'create_empty_option' => true,
        'day_attributes'      => array(
            'data-placeholder' => 'Day',
            'style'            => 'width: 20%',
        ),
        'month_attributes'    => array(
            'data-placeholder' => 'Month',
            'style'            => 'width: 20%',
        ),
        'year_attributes'     => array(
            'data-placeholder' => 'Year',
            'style'            => 'width: 20%',
        )
    )
));

For outputting, I am doing like this:

echo $this->formDateSelect($form->get('birthDate'));

The thing is that I don't know how to control the way the selects are formatted. For instance, they are currently outputted in this form: [month] [day], [year]. I want to change two things; first, I want to switch the day and month, and secondly, I want to get rid of the comma.

The helpers use the IntlDateFormatter class to format the selects. In the source code, I noticed that you can use the predefined constants for the date type when calling __invoke() [link to source code], like this:

echo $this->formDateSelect($form->get('birthDate'), IntlDateFormatter::SHORT);

Having tried all of the constants, I still cannot get the format I want.

I noticed that there are getters for the three selects, but using the formSelect view helper with each of them, empty selects are rendered because the data population is within the new view helpers. So that pretty much defeats the purpose of even using the selects individually; then I might as well just use regular selects.

Does anyone have any idea how I can gain more control on how my selects are formatted? Is it even possible without too much hassle, or should I just use three regular selects for maximum flexibility?

like image 521
ba0708 Avatar asked Feb 03 '13 01:02

ba0708


2 Answers

The format of the ViewHelper is $this->formDateSelect($element, $intlFormat, $locale).

With this being said, you should be able to create the output you want by accessing your current locale setting and insert int into the ViewHelpers __invoke(). Seeing the getLocale() of the ViewHelper implies, that the default translator from configuration is not used, so you have to set a locale explicitly for this ViewHelper, but then it should work out just fine ;)

If that doesn't work as expected for you, i may advise you to hit Bakura a message on his Blog about the new Form Features

like image 144
Sam Avatar answered Oct 28 '22 10:10

Sam


I have no idea whatsoever why anyone would want to put a random comma in between Select DOM elements when outputting a date... what can you do with it exactly?

To remove the comma and any other delimiter you can pass in via options:

"render_delimiters": false

Which I think should be false by default

like image 27
Intellix Avatar answered Oct 28 '22 10:10

Intellix