7

I make form in Controller like this below.

$form = $this->createFormBuilder($row)
->add('comment',null,array('label' => 'input comment'))

then in the twig file...

{{form_widget(form.commentToMutor)}}

It shows text input box,but it is too small.

How to change size of TextBox

Veer
  • 1,575
  • 3
  • 16
  • 40
whitebear
  • 11,200
  • 24
  • 114
  • 237

4 Answers4

22

Extending @manseuk 's answer (I don't have enough reputation to post a comment), you can also specify the html style attribute inside the form builder, if you preffer:

$form = $this->createFormBuilder($row)
   ->add('comment', null, array(
           'label' => 'input comment', 
           'attr' => array('style' => 'width: 200px')
          )
   );

Edited from html attribute for width to html style attribute.

Jim Simson
  • 2,774
  • 3
  • 22
  • 30
Dani Sancas
  • 1,365
  • 11
  • 27
  • i think it is `size` attribute, not `width` you meant? Because `` works but `` does not. – Dimitry K Jun 03 '14 at 16:55
  • I think you're right, it appears not to work. It should be something like `'attr' => array('style' => 'width: 200px')` despite it seems to work for the user I answered. In addition, you maybe wanna read the difference between `size` and `width`, just in case. http://stackoverflow.com/questions/1480588/input-size-vs-width – Dani Sancas Jun 04 '14 at 22:11
  • 4
    surely `width` should be part of the `style` attribute, so `'attr' => array('style' => 'width: 200px'` surely should work. However your original answer states: `'attr' => array('width' => '200')` which would simply produce html code `` where `width` attribute is ignored because it doesn't make sense. I think the person who accepted the answer may have tweaked your code (to `style` or `size`), but simply accepted the answer as it is. So I wonder if believe it is worthwhile to edit the answer? – Dimitry K Jun 05 '14 at 09:14
6

You could add a class to the form field :

$form = $this->createFormBuilder($row)
   ->add('comment',null,array(
           'label' => 'input comment', 
           'attr' => array('class' => 'myclass')
          )
   );

and then create the CSS relevant to that class :

.myclass {
   width: 200px;
}

Docs for the attr attribute here

Manse
  • 37,765
  • 10
  • 83
  • 108
3

Or in the twig file:

{# Define CSS class and call #}
{{ form_widget(form.commentToMutor, { 'attr': {'class': 'myclass'} }) }}

{# ... or enter width directly #}
{{ form_widget(form.commentToMutor, { 'attr': {'style': 'width: 200px'} }) }}

More here

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Skizomik
  • 125
  • 6
1

Setting the width directly did not work for me. I had to set it through the style. Of course this is an easy fix, the correct way is to use a css class like others have suggested.

$form = $this->createFormBuilder($row)
   ->add('comment', null, array(
           'label' => 'input comment', 
           'attr' => array('style' => 'width:200px')
          )
   );
Aris
  • 4,643
  • 1
  • 41
  • 38