-
-
Notifications
You must be signed in to change notification settings - Fork 94
Providing Form View Helpers with i18n value rendering #144
Comments
Hello, I am answering my own question with a solution I made myself: a class that extends TwbBundleFormElement and use a new option 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 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:
This input filter specification verifies that the user is entering a number in the french notation. For example, if he tries to enter 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. No way! My solutionSo here is my solution, maybe things could be better but it works. We will create our own FormRow View Helper that extends the very good existing TwbBundleFormElement view helper in order to apply filters to our value before rendering. For this, we need our view helper to have the Let's codingSo first we create an FilterPluginManagerAwareInterface interface for future classes that would use a
Then we create a compatible
The key thing here is the string We can new create the new TwbBundleFormElement extending the original one, implementing previous interface and using our trait.
We just override __construct and render methods. The constructor is important here because we are waiting for a new member, the The render method calls the To instanciate correctly our new
The last thing to do is to inform our
How to use?Here, we just added a very useful feature for our forms but how to use it? We have our new custom formRow view helper. It expects an
Then, in the view script, you can use this simple instruction:
The field will be filled with your float value formatted in french notation (or any other specified locale) on view script rendering. |
I think that you may suggest this behaviour to zend-form project |
I created the issue, hoping that it will grab some attention :) If you want to close the issue in your project, no problem. |
Hello,
I am not sure if this module is the right one to provide such functionnality but, as I use it and love it, I post my thoughts here.
I am french so when my form includes Float elements, I implement a Float validator and set locale to fr_FR:
All is ok for value input.
But I am very disappointed by the Zend Form View Helpers because none of them provide options to render i18n values.
When a simple
$this->formRow($form->get($price))
could be enough, I have to write:Why couldn't we set an option for filters in the form element declaration and render it correctly in the FormRow View Helper (applying the filters before rendering)?
What do you think ?
The text was updated successfully, but these errors were encountered: