Symfony2 Form Customization
Solution 1
Use the attr
array, as explained in the documentation:
$builder->add('history', 'textarea', array(
'attr' => array('cols' => '5', 'rows' => '5'),
));
Solution 2
You can set the display attributes for textarea in Twig rather than in the form:
{{ form_widget(edit_form.comment, { 'attr': {
'style' : 'width:525px',
'rows' : '4',
'cols' : '30' }} ) }}
As mentioned above, it is better practice to set this in CSS if possible however.
Solution 3
The solution for problem in Symfony 3
.
First:
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
Second: This is the code in Form:
->add('biografia', TextareaType::class, array(
'label' => 'Como me identifico, Identifiquese utilizando un máximo de 500 caracteres',
'attr' => array('class' => 'myclass')
))
Munir
A big enthusiast and passionate for technology. Graduated in Computer Science in the Federal University of Viçosa, works with web development since 2010, both in personal and professional projects. Acts as technical leader and relates with customers. Always tries to bring innovation to the working environment and leads the team into following good design patterns. Also responsible for interfaces creation and user experience improvement.
Updated on June 23, 2020Comments
-
Munir almost 4 years
I want something like this:
<textarea rows="30" cols="70" class="TextBox" style="height:100px;">
but inside my symfony2 aplication and not in the twig template i tried this:
$builder->add('history', 'textarea', array('label' => 'Nome' , 'max_length' => 1048576 , 'rows' = 30 , 'cols' = 70));
but i get "rows" and "cols" are not options...
in the twig i want something like this:
<label for="history">{{'form_anamnese_history'}}</label> {{ form_widget(form.history) }}
to be a forum-post-like textbox!
-
Adam about 9 yearsIt's better to use rows and columns in my experience, as it matches up to the line height automatically.
-
vbsessa almost 8 yearsDefining
rows
andcols
in twig view is not working on symfony 2.8. It works only during form build as @juan pointed. -
Đuro Mandinić almost 7 yearsI would say this is the best answer because physical dimensions of a form widget should be a matter of particular twig template, and not of the form type class itself.