Splendonia
Splendonia

Reputation: 1369

How to implement the event listener to a radio button on Symfony2?

This is my form

public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('nombreBD','text', array( 'required' => true, 'label' => 'Nombre Base de Datos: '))
            ->add('Servidor', 'choice', array(
                            'choices'   => array('1' => 'Si', '0' => 'No'),
                            'required'  => true,
                            'multiple'  => false,
                            'expanded'  => true,
                            'label' => 'Servidor Existente?'
                  ))
            ->add('ServidorBD','entity',
                  array ('class' => 'MonseWebBundle:ServidoresBD',
                        'multiple' => true, 
                        'required' => true, 
                        'label' => 'Servidor de Base de Datos: ',
                         'query_builder' => function(EntityRepository $er) {
                         return $er->createQueryBuilder('u')
                         ->orderBy('u.url', 'ASC');
                                                                           },
                         ))
            ;
    }

and what i'm trying to do is if the User chooses "No" in the radio button, the "ServidorBD" entity shouldn't display and it should display instead another form (loading it dynamically or redirecting the user to another url) to add a new one. Since i'm new to Symfony2, i don't quite understand how to attach the eventlistener to the "radio button" nor how to display another bit of form instead of the "ServidorBD" when this happens.

PLEASE HELP! T-T

Upvotes: 0

Views: 626

Answers (1)

egeloen
egeloen

Reputation: 5864

What you want to do is build your form dynamically according to datas you will bind on your form.

In Symfony 2.0.x or 2.1.x, the form component is not able to to alter the form structure after binding datas on it. That will be done in Symfony 2.2.

See this issue: https://github.com/symfony/symfony/issues/3767

So, currently, you can't use the form event listener to archive this use case.

Upvotes: 1

Related Questions