Symfony2 Form Customization

23,980

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')
      ))
Share:
23,980
Munir
Author by

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, 2020

Comments

  • Munir
    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
    Adam about 9 years
    It's better to use rows and columns in my experience, as it matches up to the line height automatically.
  • vbsessa
    vbsessa almost 8 years
    Defining rows and cols in twig view is not working on symfony 2.8. It works only during form build as @juan pointed.
  • Đuro Mandinić
    Đuro Mandinić almost 7 years
    I 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.