Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get input type is number using 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">

Any one suggest me. why input type is giving "text" since, I have given as a number. while building form. how i will get input type as "number" in symfony2 using form builder.

like image 983
Rajesh Vasani Avatar asked Mar 08 '12 09:03

Rajesh Vasani


3 Answers

You need to use the form type 'integer', rather than 'number'.

'number' form types should be used for floats, which require a text input.

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

$builder->add('phoneAlternative', 'integer', array('max_length'=>15));
like image 96
carlu Avatar answered Nov 03 '22 22:11

carlu


You can do this by overriding the form theme template (http://symfony.com/doc/current/book/forms.html, 'Form theming' section). 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 %}
like image 32
JF Simon Avatar answered Nov 03 '22 21:11

JF Simon


you have to do like that

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 using the input

 phoneAlternative:   <input type="Number" required> 
like image 36
Marooweb Avatar answered Nov 03 '22 23:11

Marooweb