Skip to content

[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

Merged
merged 2 commits into from
Feb 10, 2018

Conversation

linaori
Copy link
Contributor

@linaori linaori commented Apr 7, 2017

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.

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.
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Member

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!

@linaori
Copy link
Contributor Author

linaori commented Feb 9, 2018

Okay I messed up a rebase here, I'll fix it in a bit.

@linaori linaori force-pushed the missing/named-form branch from 8c83da8 to e0137f5 Compare February 9, 2018 19:25
@linaori
Copy link
Contributor Author

linaori commented Feb 9, 2018

Done, reworded the sentence slightly, let me know what you think 👍

Copy link
Contributor

@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@javiereguiluz
Copy link
Member

@iltar thanks for completing this long-standing doc request. It's finally merged now!

@javiereguiluz javiereguiluz merged commit 8d246c4 into symfony:2.7 Feb 10, 2018
javiereguiluz added a commit that referenced this pull request Feb 10, 2018
…, 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants