0

I have problems with setting form wrapper custom ID. $options['attr']['id'] dont seem to work. All options passed to createForm() method seems to be ignored... I'm working on Symfony 2.1 beta 1

Form setup:

$login_form = $this->createForm(new LoginType(), $user, array(
                    'attr' => array(
                        'id' => 'login-form'        
                    )
                ));

which is passed to the view:

{{ form_widget(login_form) }}

But it produces:

<div id="login">
    <div>
        <label class="required">Mobile</label>
        <input type="text" maxlength="255" required="required" name="login[mobile]" id="login_mobile">
    </div>
    <div>
        <label class="required">Password</label>
        <input type="text" maxlength="255" required="required" name="login[password]" id="login_password">
    </div>
</div>

So the form wrapper have id="login", instead of "login-form"

j0k
  • 22,600
  • 28
  • 79
  • 90
Maciej Pyszyński
  • 9,266
  • 3
  • 25
  • 28

3 Answers3

1

I think that it can be done in form Class in a method:

public function getName()
{
    return 'login-form';
}

Regards, Max

Max Małecki
  • 1,700
  • 2
  • 13
  • 18
1

How can this {{ form_widget(login_form) }} produce the code above like you say?

<div id="login">
    <div>
        <label class="required">Mobile</label>
        <input type="text" maxlength="255" required="required" name="login[mobile]" id="login_mobile">
    </div>
    <div>
        <label class="required">Password</label>
        <input type="text" maxlength="255" required="required" name="login[password]" id="login_password">
    </div>
</div>

This {{ form_widget(login_form) }} should render only this:

 <div>
        <label class="required">Mobile</label>
        <input type="text" maxlength="255" required="required" name="login[mobile]" id="login_mobile">
    </div>
    <div>
        <label class="required">Password</label>
        <input type="text" maxlength="255" required="required" name="login[password]" id="login_password">

That div with id="login" in your code doesn't make no sense to me, it must be you that added manually that div, so you can change the id by yourself

João Alves
  • 1,931
  • 17
  • 25
  • No, every form is wrapped in div (if you use div layout). Every simple form input extends form. So you got one DIV from main form and 2 DIVs inside from simple widgets – Maciej Pyszyński Jul 11 '12 at 18:19
0

After two years :) You can override setDefaultOptions method of AbstractType. Tested in Symfony 2.5

    class CommentType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {

              $builder
                    ->setMethod('POST')
                    ->add('text', 'textarea', array('label' => ' ',
                        'attr' => array('class' => 'form-control',
                            'placeholder' => 'Your comment')
                    ))
                      ->add('folder_id', 'hidden', array('label' => ' ',
                        'attr' => array('class' => 'form-control',
                            'placeholder' => 'Your comment')
                    ))
                      ->add('link_id', 'hidden', array('label' => ' ',
                        'attr' => array('class' => 'form-control',
                            'placeholder' => 'Your comment')
                    ))
                    ->add('save', 'button', array('label' => 'Save',
                        'attr' => array('class' => 'btn-lg btn-primary')
                            )
                    );
        }

        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Linkboard\FrontBundle\Document\comment',
                'attr' => array('id' => 'comment-form')
            ));
        }

        public function getName()
        {
            return 'comment';
        }
    }

Generates something like;

    <form name="comment" method="post" action="" id="comment-form">
    .....
    </form>
Ugur
  • 1,679
  • 14
  • 20
  • Question was about Symfony 2.1:) – Maciej Pyszyński Sep 10 '14 at 13:55
  • :) Also it was asked two years ago, but if I still find the question from Google, I thought somebody can benefit. – Ugur Sep 10 '14 at 14:51
  • Another few years later... this doesn't solve the problem. The problem is not with id attribute on form element, but the id attribute on the form element child div. –  Sep 18 '19 at 22:46