Skip to content

[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

Closed
wants to merge 3 commits into from

Conversation

Dinduks
Copy link
Contributor

@Dinduks Dinduks commented Jun 9, 2012

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.

->add('acceptRules', 'checkbox', array(
    'property_path' => false,
    'label'         => '<strong>I accept <br />the rules</strong>',
    'escape_label'  => false,
));

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.

@@ -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>
Copy link
Member

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

Copy link
Contributor Author

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 -%}
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

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

@stof , @Dinduks ok thanks.

@travisbot
Copy link

This pull request passes (merged 1c26835 into 6266b72).

@travisbot
Copy link

This pull request passes (merged 7cf0073 into 6266b72).

@webmozart
Copy link
Contributor

@fabpot What do you think about this?

@mvrhov
Copy link

mvrhov commented Jun 10, 2012

I've had so many cases where the label had to contain the html that I rather removed raw from the template itself.

@stof
Copy link
Member

stof commented Jun 10, 2012

@mvrhov I don't understand what you mean. If you remove raw from the template, you have the current behavior where the label is always escaped

@mvrhov
Copy link

mvrhov commented Jun 10, 2012

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.

@stof
Copy link
Member

stof commented Jun 10, 2012

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

@hvt
Copy link
Contributor

hvt commented Jun 20, 2012

What about having this same option for the items inside choice fields? Sometimes it is convenient to allow &euro; for example...?

@Dinduks
Copy link
Contributor Author

Dinduks commented Jul 7, 2012

Ping @fabpot.

@willdurand
Copy link
Contributor

Overriding the label template is so easy, I don't think it's a good idea to start adding custom options like that one.

@stof
Copy link
Member

stof commented Jul 7, 2012

@willdurand what you need to override for this is the rendering of the label.

@henrikbjorn
Copy link
Contributor

👎 cant you turn the autoescape of when calling the form_widget ? or something.

@stof
Copy link
Member

stof commented Jul 10, 2012

@henrikbjorn no. Autoescape is in the template. It cannot be changed when calling the template.

@stof
Copy link
Member

stof commented Oct 13, 2012

@fabpot what do you think about it ?

@webmozart
Copy link
Contributor

I agree with @willdurand that this should be solved by overriding the label template.

@webmozart webmozart closed this Oct 22, 2012
@henrikbjorn
Copy link
Contributor

@stof isnt it possible to wrap the calls to the template with {% autoescape false %} ?

@stof
Copy link
Member

stof commented Oct 22, 2012

@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

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.

9 participants