Php Symfony2 Formbuilder自动转义?
我的一些选项中有一个硬空间(Php Symfony2 Formbuilder自动转义?,php,symfony,twig,Php,Symfony,Twig,我的一些选项中有一个硬空间()。不知何故,他们在某处逃脱了。我试过: {% autoescape false %} {{ form_widget(foobar) }} {% endautoescape %} 以及 {{ form_widget(foobar)|raw }} 以及config.yml autoescape: false 然而,选择字段在这里仍然呈现为选择文本,而不是选择文本,并且在源代码中它们被编码为&;nbsp;此处选择文本 在控制器中,我有: $fo
)。不知何故,他们在某处逃脱了。我试过:
{% autoescape false %}
{{ form_widget(foobar) }}
{% endautoescape %}
以及
{{ form_widget(foobar)|raw }}
以及config.yml
autoescape: false
然而,选择字段在这里仍然呈现为选择文本
,而不是选择文本
,并且在源代码中它们被编码为&;nbsp;此处选择文本
在控制器中,我有:
$form ->add('foo', 'choice', array(
'label' => 'Foo Label',
'choices' => $fooChoices,
'required' => true));
$form = $form->getForm();
$foobar = $form->createView();
如果我print\r
$fooschoices
我得到:
Array ( [1] => 60# FooBar [5] => 60# BatBar [11] => 60# DooWop )
这显示了正确的
(注意60年代前的双空格)。在FormBuilder和渲染之间的某个地方,它正在被转义
表单生成器中是否有内置转义
我推断的是,表单视图是通过$form->createView()
呈现的,因此数据仍然没有被转换。但是,当它通过form_小部件到达Twig时,它已经被转义了。执行form_小部件(foobar)| raw
显示了这一点
编辑:我已经添加了一个变通方法作为答案,但我仍然有兴趣接受一个解释如何防止初始转义完全发生的答案。可能不是最好的解决方案,但是在表单构造函数中这样做如何(我们强制
为空格字符):
我最终创建了一个对编码HTML进行解码的细枝扩展,并将其添加为一项服务: 供应商/捆绑/扩展/细枝中的扩展
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 }}
我仍然不知道转义是在哪里执行的,因为它只在数据本身上执行(我尝试创建一个数据事件来修改表单的数据),但这至少给了我想要的最终结果。我遇到了与无线电标签相同的问题。这就解决了问题
{% for child in form %}
{% autoescape false %}
{{ child.vars.label }}
{% endautoescape %}
{{ form_widget(child) }}
{% endfor %}
您真正应该做的是超越表单标签模板
{% 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 %}
{%block form\u label%}
{%spaceless%}
{%如果标签不相同(错误)%}
{%如果不是复合%}
{%set label_attr=label_attr|merge({'for':id})%}
{%endif%}
{%如果需要%}
{%set label_attr=label_attr | merge({'class':(label_attr.class | default(“”)~'required')| trim})%}
{%endif%}
{%如果标签为空%}
{%set label=name |人性化%}
{%endif%}
{%autoescape false%}{{label}trans({},translation_domain)}{%endautoescape%}
{%endif%}
{%endspaceless%}
{%endblock form_label%}
请注意添加的自动转义部分。我可以查看表单小部件的代码吗?没有这样的功能。@David我不确定我是否了解。我使用的是根据文档呈现表单的最简单方法:我的意思是,在生成表单的类中,很抱歉存在歧义。@David编辑以提供特定代码及其接收的数据。如果需要解码引号(例如,';),可以使用
htmlspecialchars_decode($string,entu QUOTES)
我尝试了此代码并注册了细枝扩展,但仅应用于表单标签时无法解决问题。您必须将其应用于表单\u小部件
{% 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 %}