22

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!

1
  • Sizing elements is better done in your css. Commented Oct 20, 2012 at 7:11

3 Answers 3

64

Use the attr array, as explained in the documentation:

$builder->add('history', 'textarea', array(
    'attr' => array('cols' => '5', 'rows' => '5'),
));
Sign up to request clarification or add additional context in comments.

Comments

8

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.

3 Comments

It's better to use rows and columns in my experience, as it matches up to the line height automatically.
Defining rows and cols in twig view is not working on symfony 2.8. It works only during form build as @juan pointed.
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.
1

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')
      ))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.