4

My form looks like this :

class CpanelRetailerForm extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('name', 'text', array(
                    'attr' => array(
                        'class' => 'text-input',
                        'size' => '50'
                    ),
                    'required' => false
                ))
                ->add('email', 'email', array(
                    'attr' => array(
                        'class' => 'text-input',
                        'size' => '50'
                    ),
                    'required' => false
                ))
                ->add('addUser', 'checkbox', array(
                    'label' => 'Add User account',
                    'required' => false,
                    'mapped' => false
                ))
                ->add('user',new CpanelUserForm());
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TestBundle\Entity\Retailer',
            //'cascade_validation' => true
        ));
    }

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

I want to dynamically set this line from controller depending on whether addUser field is checked or unchecked.

cascade_validation' => true

Here is my controller code:

$form = $this->createForm(new CpanelRetailerForm(), new Retailer());
    $form->
    if ($this->getRequest()->isMethod('POST')) {
        $form->bind($this->getRequest());
        if ($form->get('addUser')->getData()) {
         // then set the cascade_validation to true here
        }
}

How can I do this inside controller?

My attempt : added this line in my form class:

 $builder->addEventListener(
            FormEvents::POST_SUBMIT, function(FormEvent $event) {
                $form = $event->getForm();
                $addUser = $form->get('addUser')->getData();
                $validation = false;
                if ($addUser) {
                    $validation = true;
                }
                $resolver = new OptionsResolver();
                $resolver->setDefaults(array(
                    'cascade_validation' => $validation
                ));
                $this->setDefaultOptions($resolver);
            }
    );

This didnot work for me. Although I receive data in $addUser, cascade_validation is not added

sonam
  • 3,720
  • 12
  • 45
  • 66

1 Answers1

0

How can I do this inside controller?

You can´t! Thats the simple answer. Lets take a look at following simple form class:

class TestType extends AbstractType {

    /**
     * @var boolean
     */
    private $myOption;

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $this->myOption = false;

        $builder
            ->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
                dump('formEvents::PRE_SET_DATA');
            })
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                dump('FormEvents::POST_SET_DATA');
            })
            ->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
                dump('FormEvents::PRE_SUBMIT');
            })
            ->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
                dump('FormEvents::SUBMIT');
            })
            ->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
                dump('formEvents::POST_SUBMIT');
            })
            ->add('name', TextType::class)
            ->add('send', SubmitType::class);
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setRequired(array(
            'my_option'
        ));
        $resolver->setDefaults(array(
            'my_option' => $this->setMyOption()
        ));
    }

    /**
     * @return bool
     */
    public function setMyOption() {
        dump($this->myOption);
        return $this->myOption;
    }
}

Lets take in how you render and handle a form inside a Controller:

public function formAction(Request $request) {

    $form = $this->createForm(TestType::class);

    dump('calledCreateForm');

    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {
        dump('finished');
        dump($form->getData());
        die();
    }

    return $this->render('@TestPra/Test/test_form.html.twig', array(
        'form'  => $form->createView()
    ));
    
}

After submitting the form you get the following output order:

  1. $this->setMyOption() > null
  2. FormEvents::PRE_SET_DATA
  3. FormEvents::POST_SET_DATA
  4. calledCreateForm
  5. FormEvents::PRE_SUBMIT
  6. FormEvents::SUBMIT
  7. FormEvents::POST_SUBMIT
  8. finished

The first thing allways gets called is configureOptions and because you don´t have any data of the filled form before calling handleRequest there is no way to change the options of the allready created form without manipulating Symfonys form component.

Community
  • 1
  • 1
goulashsoup
  • 2,639
  • 2
  • 34
  • 60