How to dynamically set cascading form validation from a controller

My form is as follows:

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 the controller depending on whether the checkbox of the addUser field is checked or not.

 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 the controller?

My attempt: added this line to 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 did not help me. Although I get data in $ addUser, cascade_validation is not added

+4
source share
1 answer

How can I do this inside the controller?

You can not! This is a simple answer. Let's look at the 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; } } 

Let's look at how you process and process the form inside the 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 will receive the following output order:

  • $this->setMyOption() > null
  • FormEvents::PRE_SET_DATA
  • FormEvents::POST_SET_DATA
  • calledCreateForm
  • FormEvents::PRE_SUBMIT
  • FormEvents::SUBMIT
  • FormEvents::POST_SUBMIT
  • finished

The first thing that is always called: configureOptions , and because you do not have the data of the completed form before calling handleRequest , it is impossible to change the parameters of an already created form without using the Symfonys form component.

0
source

Source: https://habr.com/ru/post/1501473/


All Articles