Skip to content

Instantly share code, notes, and snippets.

@garak
Created April 30, 2021 13:27
Show Gist options
  • Save garak/fbc48d46e5c6226fe0fe19597f5a5293 to your computer and use it in GitHub Desktop.
Save garak/fbc48d46e5c6226fe0fe19597f5a5293 to your computer and use it in GitHub Desktop.
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