Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3 createForm with construct parameters

Tags:

php

symfony

Since Symfony 2.8, you can only pass the FQCN into the controller createForm method. So, my question is, how do I pass construct parameters into the form class construct 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 in my construct arguments? These arguments are provided in the controller actions so I can't use the "Forms as services" method...

like image 893
LMS94 Avatar asked May 03 '16 08:05

LMS94


1 Answers

simply:

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

which is actually how it should have been done prior to 2.8 anyway.

edit

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

public function configureOptions( OptionsResolver $resolver ) {
    $resolver->setDefaults( [
      'arg1' => null,
      'arg2' => null,
    ] );
}
like image 160
DevDonkey Avatar answered Oct 23 '22 09:10

DevDonkey