Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[TwigBridge] Add Twig field_id() form helper #59842

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 1 commit into from
Mar 5, 2025

Conversation

Legendary4226
Copy link
Contributor

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues N/A
License MIT

Hello, I thought it would be nice to have a twig field_id(form.myField) helper instead of using the {{ form.task.vars.id }} as stated in the official documentation. https://symfony.com/doc/current/form/form_customization.html#form-rendering-variables

@Legendary4226 Legendary4226 marked this pull request as ready for review February 23, 2025 11:26
@carsonbot carsonbot added this to the 7.3 milestone Feb 23, 2025
@chalasr
Copy link
Member

chalasr commented Feb 23, 2025

Please add a proper title to your PR also

@Legendary4226 Legendary4226 changed the title Feature 1 Feat: Add twig field_id form helper Feb 24, 2025
@carsonbot carsonbot changed the title Feat: Add twig field_id form helper [TwigBridge] Feat: Add twig field_id form helper Feb 24, 2025
@symfony symfony deleted a comment from carsonbot Feb 24, 2025
@symfony symfony deleted a comment from carsonbot Feb 24, 2025
@nicolas-grekas nicolas-grekas changed the title [TwigBridge] Feat: Add twig field_id form helper [TwigBridge] Add twig field_id() form helper Feb 24, 2025
@Legendary4226
Copy link
Contributor Author

Legendary4226 commented Feb 24, 2025

I have a question relating to this PR: the FormView objects and the underlying data. Why aren't they available in the Twig context ?

For example, on a project for my company Onyl Rocks, I declared a form type based on a ViewModel that contains some entities like Organizations (as an array).
Users can belong to multiple organizations, so I have a form to handle the switch between them.
My Organizations have an image, so I wan't to display their image, assuming it's a base64 string for simplicity.
But I cannot access the underlying data on what the FormView objects are based on.

Do you think it's possible to add such a feature ? In another PR of course ^^
In my opinion, it is one of the most blocking elements when working with Twig forms to easily design great UI. Because you don't have the corresponding data close at hand. The only way is to use the "form.child.vars.value" and pass to the Twig and array of the data mapped by value used to construct the form. In twig it ends up to have some code like this:

{% for child in form.field.vars %}
    {% set myFormData = myArrayMapped[child.vars.value] %}
    ...
{% endfor %}

Without talking about the code structure, for example a global form you pass to every page like my previous example. Not hard or impossible but not pleasant for everyday use.

@OskarStark OskarStark changed the title [TwigBridge] Add twig field_id() form helper [TwigBridge] Add Twig field_id() form helper Feb 25, 2025
@chalasr
Copy link
Member

chalasr commented Mar 5, 2025

Please consider opening a new issue or PR for the feature you suggest.

@chalasr
Copy link
Member

chalasr commented Mar 5, 2025

Thank you for this nice first contribution @Legendary4226.

@chalasr chalasr merged commit 6a029ec into symfony:7.3 Mar 5, 2025
11 checks passed
@Legendary4226 Legendary4226 deleted the feature-1 branch March 6, 2025 18:08
@fabpot fabpot mentioned this pull request May 2, 2025
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.

7 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.