Vender Aeloth
Vender Aeloth

Reputation: 788

Symfony dynamic form radio button

I would like to set my form according to a radio button status but whatever the formEvents used... The value didn't change, so I can't remove or add fields...

Atm, I have this:

$builder->addEventListener(
    FormEvents::PRE_SET_DATA,
    function(FormEvent $event) use ($factory){
        $data = $event->getData();
        echo 'value :' .$data->radioAvatar;
        if ($data->radioAvatar){
                $form = $event->getForm();
                $form->add('avatar');
            }

} );

Whatever what radio is active, I will never see the new field (default value for radioAvatar is 0). Thus if default value is 1, field appears under my submit button...

my forminput:

$builder
    ->add('name', 'text',array(
    'label' =>'Public name'))
    ->add('radioAvatar', 'choice', array(
    'label' => 'Had you already sent a picture',
    'choices' => array(
        0 => 'no',
        1 => 'yes'),
    'multiple' => false,
    'expanded' => true))
    ->add('save', 'submit',array(
    'label'=> 'Update my profil',
    'attr' => array('class' => 'btn btn-primary elemtopmarg')))
;

Is it possible to update a form according to radio button and how display under the radio button group?

Best regards,

Upvotes: 0

Views: 1712

Answers (2)

Vender Aeloth
Vender Aeloth

Reputation: 788

if someone finds this post, you will find how i solved my question:

1 : You have to create a new specific form (before that all my forms were display in only one twig...)

2 : You display your form row per row then you put your encapsulated object (your new ObjectType() ) in a specific div. Then you add an other specific div for displaying the field 'entity')

3 : Insert javascript for showing/hidding specifig div according to radio button

4 : adapt your controller.

I don't think it's the best way to do that. But I didn't find something good on the Internet, if you find a better way don't hesite to answer or contact me. :)

Best regards,

Upvotes: 1

Airam
Airam

Reputation: 2058

I've modified forms in a similar way like you've tried. My code has some differences:

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function(FormEvent $event) {
            $form = $event->getForm();

            $data = $event->getData();
            if($data->getName() != NULL){
                $form->add('surname');
            }
        }
    );

This worked for me.

If you want to display this field under the radio button (in my case under the 'name' field), you could do this in your form:

$builder
    ->add('name')
    ->add('surname', 'hidden');

And later in the listener, you could change the type of the field:

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function(FormEvent $event) {
            $form = $event->getForm();

            $data = $event->getData();
            if($data->getName() != NULL){
                $field = $builder->get('surname');
                $field->setType('text');
            }
        }
    );

This last example is not tested, but I think that it should work.

I hope this be useful for you.

Kind regards.

Upvotes: 0

Related Questions