How do I get the input type is a number using a form in symfony2?

This is my form builder code

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('phoneAlternative', 'number',array('max_length'=>15));
    $builder->add('emailPersonal', 'email');
    $builder->add('addressCurrent', 'textarea');
}

      

This is html markup

<input id="employee_phoneAlternative" type="text" maxlength="15" required="required" name="employee[phoneAlternative]" class="valid">

      

Does anyone suggest to me. why is the input type giving "text" as I gave a number. when creating a form. how do i get the input type as "number" in symfony2 using the form builder.

+3


source to share


3 answers


You need to use the form type integer 'and not number .

Form types

'number' should be used for floats that require input text

.



So, the correct line of code in your case would be:

$builder->add('phoneAlternative', 'integer', array('max_length'=>15));

      

+16


source


You can do this by overriding the form theme template ( http://symfony.com/doc/current/book/forms.html , section "Forming a theme"). In your example, the block to create is 'number_widget':



{% block number_widget %}
{% spaceless %}
    {% set type = type|default('number') %}
    {{ block('input') }}
{% endspaceless %}
{% endblock number_widget %}

      

+2


source


you need to do this

public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('phoneAlternative', 'integer',array('max_length'=>15));
$builder->add('emailPersonal', 'email');
$builder->add('addressCurrent', 'textarea');
}

      

or you can create your form in html.twig with input

 phoneAlternative:   <input type="Number" required> 

      

+1


source







All Articles