Zend Framework 2 DateSelect/MonthSelect formatting
Asked Answered
C

3

6

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?

Cigarette answered 3/2, 2013 at 1:36 Comment(0)
A
2

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

Afraid answered 3/2, 2013 at 12:9 Comment(1)
I was just assuming that it was using the locale from my configuration so I left it out. Changing the locale, it doesn't always give me what I want (for instance, for my country it gives me a format that I don't like). However, I was lucky that for my target language, the formatting is exactly how I want it! I did like this: echo $this->formDateSelect($this->form->get('birthDate'), IntlDateFormatter::LONG, 'ro_RO');. Thank you very much for your answer - it is very appreciated!Cigarette
V
2

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

Vasyuta answered 29/5, 2013 at 8:32 Comment(0)
G
0

As Sam mentions you can just update the locale settings in your php.ini

For example I have this in my php.ini

intl.default_locale = en_Gb

This formats the DateSelect element in the Day / Month / Year format required for my UK projects.

Gorden answered 16/12, 2014 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.