Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Render same field multiple times #26531

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

Closed
ostrolucky opened this issue Mar 14, 2018 · 11 comments
Closed

[Form] Render same field multiple times #26531

ostrolucky opened this issue Mar 14, 2018 · 11 comments

Comments

@ostrolucky
Copy link
Contributor

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version 4.1

This is a common problem. Only workarounds exists is not using form_* functions for subsequent fields.

Wanted to solve this by doing

{{ form_row(form.save) }}
{% do form.save.setRendered(false) %}
{{ form_row(form.save) }}

quickly found setRendered does not accept arguments and PRs for multiple different use cases has been rejected1 in2 past3. Is this use case good enough for this change now?

This is specifically very useful to split form visually to two, where one block is just for RepeatedType and SubmitType. Here was the requirement from our designer

37027104-b990ff70-2130-11e8-9f2b-469c0a1c2b03

If I could render same field twice, I could reuse same button field and be done with it.

@javiereguiluz
Copy link
Member

Question for Form experts: is this limitation just to allow form_rest() and the closing form tag to know which fields have been already rendered ... or is this limitation needed for some other strong reasons? Thanks!

@jvasseur
Copy link
Contributor

There is the limitation of ids that are supposed to stay unique in the page, this could lead to labels not being correctly linked to their inputs.

@yceruto
Copy link
Member

yceruto commented Mar 14, 2018

This is specifically very useful to split form visually to two

By adding another submit button with different name and same label shouldn't be enough to cover the use case?

@ostrolucky
Copy link
Contributor Author

Yes, that's what I ultimately did. Not a fan of duplications tho.

@ogizanagi
Copy link
Contributor

A workaround for this is to set a var:

{% set saveBtn = form_row(form.save) %}

and rendering it twice (or as much as you need).

@linaori
Copy link
Contributor

linaori commented Mar 15, 2018

If you want to render the same form multiple times, you can use named forms for this afaik. Your screenshot is too small to clearly see how or what.

@mickaelandrieu
Copy link
Contributor

A workaround for this is to set a var:

You'll duplicate the id everytime you re-use the form field.

@grachevko
Copy link
Contributor

Symfony best practice: Add buttons in the templates, not in the form classes or the controllers.

@ostrolucky
Copy link
Contributor Author

ostrolucky commented Apr 1, 2018

Ok plenty things have been answered and even some good workarounds given. I am going to ask different question now. What's the reason for returning empty string for fields which were already rendered, if user tries to render them multiple times, instead of throwing exception with explanation? How about deprecating this silent behaviour?

@ogizanagi
Copy link
Contributor

@ostrolucky: It would make sense to me, indeed.

@ostrolucky
Copy link
Contributor Author

See #26531

If we disallow calling this for same field twice, we are opening up opportunity to change this behaviour in future.

E.g. in Symfony 6 we can make it no longer throw exception and allow to render same field more times, while shifting responsibility of making sure IDs do not collide to user, or even generate different IDs for subsequent fields. Currently this can't be changed due to BC.

@fabpot fabpot closed this as completed Jun 25, 2018
fabpot added a commit that referenced this issue Jun 25, 2018
…y string (ostrolucky)

This PR was squashed before being merged into the 4.2-dev branch (closes #27247).

Discussion
----------

[Form] Deprecate `searchAndRenderBlock` returning empty string

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #26531
| License       | MIT
| Doc PR        | -

I would like to remove this silent behavior, because it's confusing

Commits
-------

02f2f0e [Form] Deprecate `searchAndRenderBlock` returning empty string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants