Reputation: 5084
Actually I'm rather confused about the best way to pass arguments to a form. As far as I know, there are three ways:
To turn the form class into a service
To pass arguments through the array options
To pass argument through the constructor of the form class
What would be the best/cleanest way?
Upvotes: 0
Views: 124
Reputation: 812
In my opinion:
Upvotes: 1
Reputation: 1499
For the majority of my forms, I create a separate form class. When I need to render the form with prepopulated data, I load the entity in my controller and pass that entity when construction the form. For example, if I have an entity called $user linked to registration entity, I will populate my form like this:
$registration = $user->getRegistration();
$form = $this->createForm(new RegistrationType(), $registration, array());
Then I render the $form in twig:
return $this->render(
'AcmeAccountBundle:Account:register.html.twig',
array('form' => $form->createView())
);
Upvotes: 0