|
20 | 20 | use Symfony\Component\Form\FormBuilderInterface;
|
21 | 21 | use Symfony\Component\Form\FormFactoryInterface;
|
22 | 22 | use Symfony\Component\Form\FormInterface;
|
| 23 | +use Symfony\Component\Form\FormView; |
23 | 24 | use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
24 | 25 | use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
|
25 | 26 | use Symfony\Component\HttpFoundation\JsonResponse;
|
@@ -267,21 +268,33 @@ protected function render(string $view, array $parameters = [], Response $respon
|
267 | 268 | }
|
268 | 269 |
|
269 | 270 | /**
|
270 |
| - * Renders a view for a form. |
| 271 | + * Renders a view and sets the appropriate status code when a form is listed in parameters. |
271 | 272 | *
|
272 |
| - * The FormView instance is passed to the template in a variable named |
273 |
| - * "form" (can be changed via $formVar argument). |
274 |
| - * If the form is invalid, a 422 status code is returned. |
| 273 | + * If an invalid form is found in the list of parameters, a 422 status code is returned. |
275 | 274 | */
|
276 |
| - protected function renderForm(string $view, FormInterface $form, array $parameters = [], Response $response = null, string $formVar = 'form'): Response |
| 275 | + protected function renderForm(string $view, array $parameters = [], Response $response = null): Response |
277 | 276 | {
|
278 |
| - $response = $this->render($view, [$formVar => $form->createView()] + $parameters, $response); |
| 277 | + if (null === $response) { |
| 278 | + $response = new Response(); |
| 279 | + } |
| 280 | + |
| 281 | + foreach ($parameters as $k => $v) { |
| 282 | + if ($v instanceof FormView) { |
| 283 | + throw new \LogicException(sprintf('Passing a FormView to "%s::renderForm()" is not supported, pass directly the form instead for parameter "%s".', get_debug_type($this), $k)); |
| 284 | + } |
279 | 285 |
|
280 |
| - if ($form->isSubmitted() && !$form->isValid()) { |
281 |
| - $response->setStatusCode(422); |
| 286 | + if (!$v instanceof FormInterface) { |
| 287 | + continue; |
| 288 | + } |
| 289 | + |
| 290 | + $parameters[$k] = $v->createView(); |
| 291 | + |
| 292 | + if (200 === $response->getStatusCode() && $v->isSubmitted() && !$v->isValid()) { |
| 293 | + $response->setStatusCode(422); |
| 294 | + } |
282 | 295 | }
|
283 | 296 |
|
284 |
| - return $response; |
| 297 | + return $this->render($view, $parameters, $response); |
285 | 298 | }
|
286 | 299 |
|
287 | 300 | /**
|
|
0 commit comments