-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] improve AbstractController::renderForm() #41190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Shouldn't this be opt-in, as it's technically a BC break for apps already passing a form as a variable? |
I was thinking the same thing 👍 |
From the HTTP pov, this is a bug fix. |
If I understand correctly, I'm not not able to pass a Form to my view (a FormView will always be passed instead). This is weird, I agree, I'm not sure anyone would have the usage of passing a Form instead of a FormView. |
One possible issue: if exception is handled inside controller and status code is set to 500, this could overwrite it to 422. Not really sure which one should be correct in that case. (probably the 500)
|
And yes, I'm assuming no app is going to pass a FormInterface to twig, at least not as a root variable. I think this is safe. |
a24691c
to
caaeac8
Compare
Let's be on the safe side, the renderForm() method is back. PR and description updated. |
caaeac8
to
4c45876
Compare
fixed |
src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with minor comment
4c45876
to
563356e
Compare
563356e
to
e244d31
Compare
$response = new Response(); | ||
} | ||
|
||
foreach ($parameters as $k => $v) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it could be better to pass explicitly the form object since it can save us from cycling all the parameters.
<?php
protected function renderForm(string $view, FormInterface $form, array $parameters = [], Response $response = null): Response
Moreover, it improves simplest cases: instead of calling $this->renderForm('view.html.twig', ['form' => $form)
you can use this->renderForm('view.html.twig', $form)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@garak this was discussed on other PR.
There are 2 problems with this approach:
- It makes less obvious what is variable name of form in view.
- Doesn't work when have more than 1 form. (Typical use-case: Profile editing: 1 form for simple fields (name), second for for phone change, 3rd form for email change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's exactly what this PR iterates over, see patch. Iterating makes the helper compatible with controllers that have multiple forms as parameters, and saves from creating any convention to name that $form parameter inside the list of $parameters (see removed $formVar)
Even better than #41178, this requires a simple change on apps, and is compatible with multiple forms.
Usage:
In 5.4, we could even deprecate passing a FormView to render() so that we can always set the 422.