Maciej Pyszyński
Maciej Pyszyński

Reputation: 9374

How to customize main form ID in Symfony2

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"

Upvotes: 0

Views: 2615

Answers (3)

Ugur
Ugur

Reputation: 1729

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>

Upvotes: 0

Jo&#227;o Alves
Jo&#227;o Alves

Reputation: 1941

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

Upvotes: 1

Max Małecki
Max Małecki

Reputation: 1702

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

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

Regards, Max

Upvotes: 1

Related Questions