-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Form] Added a small explanation about named forms #7766
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
forms.rst
Outdated
the form type. To differentiate between the forms, you can create a form using | ||
:method:`FormFactoryInterface::createNamed() <Symfony\\Component\\Form\\FormFactoryInterface>`. | ||
Doing so, will change the html name of the form and Symfony will be able to see | ||
which of the forms was submitted, and handle it accordingly. |
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 found it confusing, it sounds weird to achieve this, we would need to create different forms in a controller from the same type, pass different views to render them distinctly in a template, without forgetting to handle the request with all the form until one is submitted... very strange!
Why would we need to know which one is submitted if there are the same? I don't get it.
When ONE form from one type needs to be rendered many times, why the name would be different? To handle many rendering using the form
twig function of one form view, we have to work around the "rendered" attribute, this can be done using:
{% set rendered_form = form(form) %}
{{ rendered_form }} {# as many times as needed #}
To me this is only useful to explicit the name, which is normally generated from the type class name, nothing more.
The only case I see this could be possible is when the same type is used for many forms but with different options that deeply changes the form's configuration, and in the same controller. Should we really document such edge cases without making it sound more complex than it really is?
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.
the createNamed
is currently 100% undocumented while there are dozens of questions about it on stack overflow, hence I'd like to see it in the docs.
Personally I would not really use this but put everything in 1 form and add a collection, but it seems like others do use it.
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 understand the need to document this method, but I propose to just say something like:
A form name is generated from the type class name, if you want to set it explicitly use ..., you can event suppress it by explicitly set it to an empty string.
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.
@iltar will you have some time to reword this contribution as a short .. note::
somewhere with the contents proposed by Jules ("A form name is generated from the type class name, if you want to set it explicitly use ..., you can event suppress it by explicitly set it to an empty string.")? Thanks!
0d4305a
to
8c83da8
Compare
Okay I messed up a rebase here, I'll fix it in a bit. |
8c83da8
to
e0137f5
Compare
Done, reworded the sentence slightly, let me know what you think 👍 |
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.
Thanks!
@iltar thanks for completing this long-standing doc request. It's finally merged now! |
…, javiereguiluz) This PR was merged into the 2.7 branch. Discussion ---------- [Form] Added a small explanation about named forms I've added a small explanation on how to add the same form multiple times to the same page, by using named forms. Should fix #4090. Note that this feature was already added in at least one of the patches in 2.1. Commits ------- 8d246c4 Minor reword e0137f5 Added a note about named form types
I've added a small explanation on how to add the same form multiple times to the same page, by using named forms. Should fix #4090.
Note that this feature was already added in at least one of the patches in 2.1.