-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add escape_label option #4543
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
@@ -1,3 +1,3 @@ | |||
<?php if ($required) { $label_attr['class'] = trim((isset($label_attr['class']) ? $label_attr['class'] : '').' required'); } ?> | |||
<?php if (!$compound) { $label_attr['for'] = $id; } ?> | |||
<label <?php foreach ($label_attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label> | |||
<label <?php foreach ($label_attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php if ($escape_label) ?><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?><?php else ?><?php echo $view['translator']->trans($label, array(), $translation_domain) ?><?php endif ?></label> |
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.
why closing the PHP tag and opening again just after ? It seems weird
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.
They were on different lines before I noticed that everything needs to be on a single one to not break the tests, so I just joined the lines without paying attention to that detail. I will fix that.
@@ -225,7 +225,13 @@ | |||
{% if required %} | |||
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %} | |||
{% endif %} | |||
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label> | |||
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}> | |||
{%- if escape_label -%} |
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.
this code is in a spaceless
tag. So you don't need to use {%-
, {%
is enougth
it's the same for other lines.
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.
It seems that that block isn't that spaceless. It still renders spaces, and makes tests fail.
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.
@lyrixx spaceless
removes spaces between tags, not between a tag and some text
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.
@fabpot What do you think about this? |
I've had so many cases where the label had to contain the html that I rather removed raw from the template itself. |
@mvrhov I don't understand what you mean. If you remove |
It seems that since last form refactoring unescaped values are default. Before that I had redefine the label template with added |raw. I was just to quick in answering this. |
labels are escaped in the core theme, unless you disabled the auto-escaping in Twig: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig#L220 |
What about having this same option for the items inside choice fields? Sometimes it is convenient to allow |
Ping @fabpot. |
Overriding the label template is so easy, I don't think it's a good idea to start adding custom options like that one. |
@willdurand what you need to override for this is the rendering of the label. |
👎 cant you turn the autoescape of when calling the form_widget ? or something. |
@henrikbjorn no. Autoescape is in the template. It cannot be changed when calling the template. |
@fabpot what do you think about it ? |
I agree with @willdurand that this should be solved by overriding the label template. |
@stof isnt it possible to wrap the calls to the template with |
@henrikbjorn the autoescaping is done when compiling the template. The way you use this template cannot influence it. You would have to modify the source of the template itself, not to put an autoescape tag in the caller template |
Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -
License of the code: MIT
Documentation PR: symfony/symfony-docs#1442
This option simply tells Twig not to escape a form's label and is useful when wanting to use a HTML tag inside a label.
It is set to false by default, and thus doesn't break BC.
I'm aware that this option may lead to writing dirty code sometimes, but I believe the choice is up to the developer ; it can't hurt if one knows what he's doing. Besides, it'd save time when writing simple forms.