Symfony 3 createForm with build options

With Symfony 2.8, you can only pass FQCNs to the controller’s createForm method. So my question is: how do I pass the design parameters to the design of the form class when I create the form in the controller?

<Symfony 2.8 I could do (MyController.php):

$this->createForm(new MyForm($arg1, $arg2)); 

Symfony 2.8+ I can only do (MyController.php):

 $this->createForm(MyForm::class); 

So how can I pass construct arguments? These arguments are provided in the actions of the controller, so I cannot use the "Forms as Services" method ...

+13
source share
1 answer

simply:

 $this->createForm(MyForm::class, $entity, ['arg1' => $arg1, 'arg2' => $arg2]); 

which actually had to be done before 2.8 anyway.

change

based on your comment, you need to set default values ​​in the class type itself:

 public function configureOptions( OptionsResolver $resolver ) { $resolver->setDefaults( [ 'arg1' => null, 'arg2' => null, ] ); } 
+12
source

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


All Articles