'Symfony 3.4: NumberType & MoneyType are rendering text type html input instead of number type input
I'm working with Symfony 3.4 and I get an issue with NumberType Form Field which is rendering a text input:
ElementArrivageType.php:
->add('prixUnit', NumberType::class, array(
'scale' => 3,
'attr' => array(
"min" => 0,
"scale" => 3,
"step" => 0.001,
"placeholder" => "0.000",
)
))
Twig:
{{ form_widget(form.prixUnit, {'attr': {'class': 'form-control'}}) }}
Result F12:
<input type="text" id="appbundle_arrivage_elementArrivages_2_prixUnit" name="appbundle_arrivage[elementArrivages][2][prixUnit]" required="required" min="0" scale="3" step="0.001" placeholder="0.000" class="form-control prixUnit" value="500.011">
When documenting I found a Solution that doesn't worked for me :
Twig :
{% block number_widget %}
<div class="number_widget">
{% set type = type|default('number') %}
{{ block('form_widget_simple') }}
</div>
{% endblock %}
{{ form_start(form) }}
{{ form_row(form) }}
{{ form_end(form) }}
The only solution that I found is a not clean solution:
//forcing type when loading DOM:
$('#appbundle_arrivage_elementArrivages_2_prixUnit').prop('type', 'number');
Does anyone have a clean solution ?
Solution 1:[1]
Based on the Symfony's documentation:
Renders an input text field and specializes in handling number input.
https://symfony.com/doc/3.4/reference/forms/types/number.html
So it's ok if you have a text input. Symfony will, on submit, check if it's a number and not a string
Solution 2:[2]
For me, it's resolved by Form Functions and Variables Reference:
{{ form_row(form.price, {'type': 'number'}) }}
Documented here: https://symfony.com/doc/current/form/form_customization.html#method-2-inside-a-separate-template
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zahori |
| Solution 2 | marino andriamialy |
