-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Implement Twig helpers to get field variables #38307
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
3a3a33d
to
da06085
Compare
Here's a completely different proposal: what if we forget about Twig filters and functions and just design form Twig variables like this:
In practice, using the same example as above: <input
name="{{ form.username.name }}"
value="{{ form.username.value }}"
placeholder="{{ form.username.label|trans }}"
class="form-control"
/>
{% for error in form.username.errors %}
<div class="text-danger mb-2">
{{ error }}
</div>
{% endfor %}
<select name="{{ form.country.name }}" class="form-control">
<option value="">{{ form.country.label|trans }}</option>
{% for label, value in form.country.choices %}
<option value="{{ value }}">{{ label|trans }}</option>
{% endfor %}
</select>
{% for error in form.country.errors %}
<div class="text-danger mb-2">
{{ error }}
</div>
{% endfor %} |
i think we should aim for improved form vars from the backend yes. AFAIK #35082 is basically asking for |
@javiereguiluz my main concern about this is DX: 1/ Autocompletion for dynamic variables is hard (whereas for functions it's already handled by Symfony IDE plugins) @ro0NL The |
I see an issue with On a side note, our existing API for FormView already suffers from conflict issues in Twig. The |
@tgalopin @stof in my comment I was biased by EasyAdmin, where we pass a simple @stof about what does |
1c34176
to
c95592f
Compare
I just pushed an update. I added an example in the description on how to use this PR to display optgroups. @nicolas-grekas It now returns translated strings if a translator is provided and direct values otherwise. I also added the wiring with the TwigBundle. |
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 would absolutely find value in such functions, mainly because of the translation thing. So +1 on my side.
Here is a table with some comparison:
Also a big +1 for |
The translation features look very nice 👍 Here's a question: if this is merged, what will be the strategy to document Symfony Forms? Only show I haven't looked into the internals of this proposal, so I don't know if things are configurable or not. For example:
This would be confusing 😐 |
I really love the idea, but one thing I would like to see as well is a helper that takes a label and a translation domain ( {%- if translation_domain is same as(false) -%}
{%- if label_html is same as(false) -%}
{{- label -}}
{%- else -%}
{{- label|raw -}}
{%- endif -%}
{%- else -%} It's great if this is handled for the core properties, but often times, you need it for custom |
@yceruto note that you missed the
@apfelbox I don't understand what you propose: isn't this already what the @javiereguiluz my point of view is that we should aim at relying less on the In the short term though, it's not feasible to only have inline functions, it would be a way too big change for a only small benefit. My POV in the short term is therefore that we should introduce these field functions at the beginning of the documentation (the first thing we describe relative to how to render forms), and then we mention additional form functions that are useful (form_start, form_rest, form_end, ...). IMHO we don't need to mention the |
c95592f
to
1dd1b83
Compare
I just updated the PR following @yceruto review |
@tgalopin I would like a default filter / function, that basically does the So basically instead of {%- if translation_domain is same as(false) -%}
{%- if label_html is same as(false) -%}
{{- label -}}
{%- else -%}
{{- label|raw -}}
{%- endif -%}
{%- else -%}
{%- if label_html is same as(false) -%}
{{- label|trans(label_translation_parameters, translation_domain) -}}
{%- else -%}
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
{%- endif -%}
{%- endif -%}
{# .. snip .. #}
{%- if translation_domain is same as(false) -%}
{%- if placeholder_html is same as(false) -%}
{{- placeholder -}}
{%- else -%}
{{- placeholder|raw -}}
{%- endif -%}
{%- else -%}
{%- if placeholder_html is same as(false) -%}
{{- placeholder|trans(placeholder_translation_parameters, translation_domain) -}}
{%- else -%}
{{- placeholder|trans(placeholder_translation_parameters, translation_domain)|raw -}}
{%- endif -%}
{%- endif -%}
{# ... snip ... #}
(same for `help` here) I would like to just be able to {# name tbd #}
{{- form_translate_label(label, translation_domain, label_translation_parameters, label_html) -}}
{{- form_translate_label(placeholder, translation_domain, placeholder_translation_parameters, placeholder_html) -}}
{# ... #} So encapsulate the whole logic of
that needs to be duplicated throughout the whole template (especially if you have additional translatable labels (like I have above with my custom |
Putting the handling of On a side note, |
@apfelbox as you are already in the view, I don't see the use case for that: either you want to be generic, in which case @stof thanks, fixed it. |
1dd1b83
to
3941d70
Compare
I know that 😉
The use case is that you might have a lot of different translatable elements and then you have to copy the same code throughout your whole template.
I agree. Especially as the "proper" solution for this specific issue would be a different solution altogether 😅 |
I don't think these functions will replace the actual form_* functions somehow, but they would improve the way we customize form themes. |
Thank you @tgalopin. |
Would be great if |
I thought about it but I don't think it makes much sense: you already are in the view, you can add attributes easily. Feel free to open a PR with what you have in mind though, I may have missed a use case :) |
…jmsche) This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Form] Add some basic docs for Twig Form field helpers Closes #14308 Refers to the feature added in symfony/symfony#38307 Hi, So these Twig helpers have been added for a long time now (2+ years) but unfortunately it's still undocumented. As a "quick fix", I took the liberty to "steal" the text + example from the [related blog post](https://symfony.com/blog/new-in-symfony-5-2-form-field-helpers) so they will at least be referenced in the docs. Commits ------- 9e21175 [Form] Add some basic docs for Twig Form field helpers
Designing Symfony Forms has always been difficult, especially for developers not comfortable with Symfony or Twig. The reason behind this difficulty is that the current
form_*
helper functions, while providing a way to quickly render a form, are hiding the generated HTML behind a notation specific to Symfony.HTML standards introduced many new attributes since the Form component was created, from new constraints to how should inputs be displayed, treated by screen readers, etc.
I propose to introduce a series of new Twig functions to help create more flexible forms without the hurdle of having to use
form_*
functions. I called these methodsfield_*
because they aim at rendering only the tiny bits of strings necessary to map forms to the Symfony backend.The functions introduced are:
field_name
returns the name of the given fieldfield_value
returns the current value of the given fieldfield_label
returns the label of the given field, translated if possiblefield_help
returns the help of the given field, translated if possiblefield_errors
returns an iterator of strings for each of the errors of the given fieldfield_choices
returns an iterator of choices (the structure depending on whether the field uses or doesn't use optgroup) with translated labels if possible as keys and values as valuesA quick example of usage of these functions could be the following:
There are several advantages to using these functions instead of their
form_*
equivalents:The
form_*
functions are still usable of course, but I'd argue this technique is actually easier to read and understand.