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.
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));
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 %}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With