Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing Form View Helpers with i18n value rendering #53

Open
michalbundyra opened this issue Jan 15, 2020 · 3 comments
Open

Providing Form View Helpers with i18n value rendering #53

michalbundyra opened this issue Jan 15, 2020 · 3 comments

Comments

@michalbundyra
Copy link
Member

Description of the problem:

For example, say we have a text field that is used to store float numbers and our application is in french (yes, it's my native language :). In database, it is stored as float, like 4112.156. In a french notation, this number should appear as 4 112,156.

In that case, if you want to do things the right way you expect the user to enter numbers in a french notation. So you add an input filter and a validator in your form like this:

'my_localized_number_field' => array(
    'allow_empty' => true,
    'required' => false,
    'filters' => array(
        array(
            'name' => 'NumberParse',
            'options' => array(
                'locale' => 'fr_FR',
                'style' => \NumberFormatter::DECIMAL,
            ),
        ),
    ),
    'validators' => array(
        array(
            'name' => 'IsFloat',
            'options' => array(
                'locale' => 'fr_FR',
            ),
        ),
    ),
),

This input filter specification verifies that the user is entering a number in the french notation. For example, if he tries to enter 4112.156, it will be rejected.
If the user enters 4 112,156, the value will be accepted, then parsed to a float and finally saved in database as a real float type 4112.156.

Good!

Now the user want to modify this value and opens the form again. When the form will be populated by the database row, the field value will be written in the native float notation.
Using the original formRow view helper, as the field is filled with a native float notation, when the user will try to submit the form again without any modification, it will be rejected because the value is 4112.156 and the form expects 4 112,156.

No way!

As suggested there, can you see my issue neilime/zf2-twb-bundle#144 where I offer a solution to this problem. I think a similar mechanism should be included in zf2 in order to avoid adding 5 lines of code for each i18n related fields as soon as an application is multi-lingual.

Thank you.


Originally posted by @berturion at zendframework/zend-form#10

@michalbundyra
Copy link
Member Author

Wouldn't it be more convenient to let a hydrator strategy format the number according to a given locale?

Say you want to display the data as plain html before populating a form you'd also want it to show up in a localized format. Now you'd have to filter the value in two different places.


Originally posted by @demichl68 at zendframework/zend-form#10 (comment)

@ALTAMASH80
Copy link
Contributor

My dumb question will be, how will someone handle multiple languages? For example, sites which have multi versions. How will they handle this situation which you've proposed? Thanks!

@ALTAMASH80
Copy link
Contributor

After going through the code of laminas-form version 2.16.x. The label option gets translated if a local is set. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants