kironet
kironet

Reputation: 935

Symfony 4 - Multiple forms in one controller

What is the best practice if I want to use multiple forms in one page/controller?

Right now I have multiple actions and I'm including them in twig, but I don't think this is good solution.

Thanks

Upvotes: 0

Views: 1990

Answers (1)

Francesco Abeni
Francesco Abeni

Reputation: 4265

Assuming you actually want different, separate forms in your page, you can create many forms in your controller and pass them to Twig, e.g.:

public function mypageAction()
{
    $userForm = $this->createForm(UserForm::class, new User);
    $companyForm = $this->createForm(CompanyForm::class, new Company);
    return $this->render('mypage.html.twig', [
        'userForm' => $userForm,
        'companyForm' => $companyForm,
    ]);
}

Of course handling submissions will require separate actions, one for each form.

Upvotes: 3

Related Questions