Skip to content

Instantly share code, notes, and snippets.

@garak
Created Apr 30, 2021
Embed
What would you like to do?
renderForm helper method for Symfony base controller
<?php
protected function renderForm(string $template, FormInterface $form, array $params = []): Response
{
$code = $form->isSubmitted() && !$form->isValid() ? Response::HTTP_UNPROCESSABLE_ENTITY : Response::HTTP_OK;
return $this->render($template, array_merge($params, ['form' => $form->createView()]), new Response(null, $code));
}
@garak
Copy link
Author

garak commented Apr 30, 2021

With such method, you can just use $this->renderForm('mytemplate.html.twig', $form).
See https://symfony.com/blog/new-in-symfony-5-3-form-handler-helper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment