Symfony2: trying to add type = "number" attribute to form field
I have this field,
->add('number', 'number', array(
'label' => false,
'attr' => array('class' => 'producto', 'type' => 'number')))
I was expecting to add an attribute type="number"
, increase and decrease the number inside with small up / down arrows, but I get an input with a type = "text" attribute:
<input type="text" value="11" class="producto form-control" required="required" name="project_backendbundle_pedido[pedidoSubitems][0][number]" id="project_backendbundle_pedido_pedidoSubitems_0_number">
source to share
The attribute type
is set using the selected widget type. To get type="number"
, you need to choose integer
as the type of widgets. Confused, I know ..
But take a look at the official symfony page . Therefore, your code should look like this:
->add('number', 'integer', array(
'label' => false,
'attr' => array('class' => 'producto'),
))
source to share
To have a type number you need to do (for symfony 2.8):
Use the following options in the form builder:
->add('fixedCharge', 'number', array(
'required' => true,
'rounding_mode' => 0,
'precision' => 2,
'scale' => 7,
'attr' => array(
'min' => -90,
'max' => 90,
'step' => 0.01,
),
))
The number is clearly indicated in the twing:
{{ form_widget(form.fixedCharge, { 'type':'number' }) }}
Additionally, for browsers that ignore HTML5 validation, you can also add server side validation to the object field:
@Assert\Type(type="float", message="must be a numeric value")
source to share