LMS94
LMS94

Reputation: 1036

Symfony 3 createForm with construct parameters

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...

Upvotes: 19

Views: 15090

Answers (1)

DevDonkey
DevDonkey

Reputation: 4880

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,
    ] );
}

Upvotes: 20

Related Questions