zendframework / zend-form

Form component from Zend Framework
BSD 3-Clause "New" or "Revised" License
69 stars 87 forks source link

Applying filters to form elements with i18n validators on rendering with form view helpers #91

Open GeeH opened 8 years ago

GeeH commented 8 years ago

This issue has been moved from the zendframework repository as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html


Original Issue: https://api.github.com/repos/zendframework/zendframework/issues/7511 User: @berturion Created On: 2015-05-11T08:54:59Z Updated At: 2015-11-06T21:38:46Z Body Hello, I am facing a problem when rendering forms containing existing values that have i18n input filters.

Here is an example with a Float: 1 - I fill a form with a float in french format (comma as decimal separator) and submit it 2 - The form validates the float in french format => OK 3 - I reopen the form to edit the float using FormRow view helper 4 - The float is rendered in english format (dot as decimal separator) 5 - If I submit the form as is, with no modification, the float field get an input validation error because the float has been redered in english format => KO

My question is: Why couldn't we set a 'filters' option in the form element declaration and render it correctly in the FormRow View Helper and others (applying the filters before rendering) like this?

    $this->add(array(
        'name' => 'price',
        'type' => 'text',
        'options' => array(

        ),
        'filters' => array(
            array(
                'name' => 'NumberFormat',
                'locale' => 'fr_FR',
                'style' => \NumberFormatter::DECIMAL
            )
        ),
    ));

In that way, we could render i18n form values in the same way as they should be entered.

What do you think ?

EDIT:

The workaround I found is this:

$price = $form->get('price');
$origValue = $price->getValue();
$price->setValue($this->numberFormat($origValue, \NumberFormatter::DECIMAL, \NumberFormatter::TYPE_DEFAULT, 'fr_FR'));
echo $this->formRow($price);

But I would really want to reduce view scripts and be able to use this simple line:

$this->formRow($form->get($price))

michalbundyra commented 4 years ago

This repository has been closed and moved to laminas/laminas-form; a new issue has been opened at https://github.com/laminas/laminas-form/issues/37.