Symfony2 Automatically disable the shaper?

I have hard space (  ) inside some of my choices. Somehow somewhere they run away. I tried:

{% autoescape false %}
    {{ form_widget(foobar) }}
{% endautoescape %}    

Like

{{ form_widget(foobar)|raw }}

And in the Twig in section config.yml

autoescape: false

However, the selection fields are still displayed as  Choice Text Hereinstead Choice Text Here, and in the source they are encoded as Choice Text Here

In the controller, I have:

$form   ->add('foo', 'choice', array(
            'label' => 'Foo Label',
            'choices'  => $fooChoices,
            'required' => true));
$form = $form->getForm();
$foobar = $form->createView();

If I print_r $fooChoices, I get:

Array ( [1] =>  60# FooBar [5] =>  60# BatBar [11] =>  60# DooWop )

Which shows me the correct one  (note the double space before the 60s). Somewhere between FormBuilder and rendering, it slips away.

Is there built-in escaping inside Form Builder?

, , , , $form->createView(), . , Twig form_widget, . form_widget(foobar)|raw .

: , , , .

+5
4

. .

{% for child in form %}

  {% autoescape false %}
    {{ child.vars.label }}
  {% endautoescape %}

  {{ form_widget(child) }}

{% endfor %}
+8

Twig, HTML :

Vendor/Bundle/Extensions/Twig

namespace Vendor\Bundle\Extensions\Twig;

class HTMLDecodeTwigExtension extends \Twig_Extension 
{

    public function getFilters()
    {
        return array(
            'htmldecode' => new \Twig_Filter_Method($this, 'htmldecode', array(
                'is_safe' => array('html'))
            ),
        );
    }

    // your custom function
    public function htmldecode($string)
    {
        return html_entity_decode($string);
    }

    // Name for Service
    public function getName()
    {
        return 'html_decode_twig_extension';
    }
}

Vendor/Bundle/Resources/config/services.yml

vendor_bundle.htmldecode:
    class:  Vendor\Bundle\Extensions\Twig\HTMLDecodeTwigExtension
    tags:
      - { name: twig.extension }

:

{{ form_widget(foobar)|htmldecode }}

, , ( ), , , .

+3

What you really have to do is overcoming the form_label template

{% block form_label %}
{% spaceless %}
    {% if label is not sameas(false) %}
        {% if not compound %}
            {% set label_attr = label_attr|merge({'for': id}) %}
        {% endif %}
        {% if required %}
            {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
        {% endif %}
        {% if label is empty %}
            {% set label = name|humanize %}
        {% endif %}
        {% autoescape false %}<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>{% endautoescape %}
    {% endif %}
{% endspaceless %}
{% endblock form_label %}

Note the addition of autoescape sections.

+3
source

This is probably not the best solution, but how to do it in the form constructor (we force to &nbsp;be a char space):

public function __construct() {

    foreach ($this->fooChoices as $key => $fooChoice) {

        $this->fooChoices[$key] = html_entity_decode($fooChoice, ENT_NOQUOTES, 'UTF-8');
    }
}
+1
source

All Articles