2012-07-02 3 views
5

일부 선택 옵션 내에 하드 공간 ( )이 있습니다. 여하튼, 어디 선가, 그들은 도망 가고 있습니다. 에Symfony2 Formbuilder가 자동으로 이스케이프 처리합니까?

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

뿐만 아니라

{{ form_widget(foobar)|raw }} 

등을 그리고 선택 필드는 여전히 대신 Choice Text Here Choice Text Here로 렌더링됩니다 그러나 config.yml

autoescape: false 

에서 나뭇 가지 아래에 다음과 : 나는 시도 인코딩 된 소스  Choice Text Here

컨트롤러에서

나는이 :

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

내가 print_r$fooChoices 내가 할 경우 나에게 적절한   (60 년대의 앞의 두 배의 공간을 유의)를 보여줍니다

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

. FormBuilder와 렌더링 사이의 어떤 곳에서 도망 가고 있습니다.

Form Builder 내에 내장 된 이스케이프가 있습니까?

제가 추론 한 점은 양식보기가 $form->createView()을 통해 렌더링된다는 점을 통해 데이터가 여전히 이스케이프 처리되지 않았기 때문입니다. 그러나 form_widget을 통해 Twig에 도달 할 때까지는 탈출했습니다. form_widget(foobar)|raw은 이것을 보여줍니다.

편집 : 답변으로 해결 방법을 추가했지만 초기 이스케이프가 모두 발생하지 않도록하는 방법을 설명하는 답변을 수락하는 데 여전히 관심이 있습니다. 아마

+0

양식 위젯의 코드를 볼 수 있습니까? 그러한 기능은 없습니다. – Lusitanian

+0

@ David 나는 내가 따를지는 잘 모릅니다. docs에 따라 양식을 렌더링하는 가장 간단한 방법을 사용하고 있습니다. http://symfony.com/doc/current/book/forms.html#rendering-the-form – Nick

+0

형태, 모호한 것에 대해 유감스럽게 생각합니다. – Lusitanian

답변

8

라디오 라벨과 동일한 문제가 발생했습니다. 이것은 그것을 해결합니다.form_label 템플릿을

{% 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 %} 

참고 autoescape 섹션의 추가를 타고 오버

{% for child in form %} 

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

    {{ form_widget(child) }} 

{% endfor %} 
1

하지 최고의 솔루션,하지만 양식 생성자에서 그 일에 대해 (우리는 공간 문자로 &nbsp; 강제) :

public function __construct() { 

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

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

그리고 인용 부호 (예 : ')를 해독해야하는 경우'htmlspecialchars_decode ($ string, ENT_QUOTES) '를 사용할 수 있습니다. – geoB

3

내가 인코딩 디코딩하는 나뭇 가지 확장을 만들어 결국 한을 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'; 
    } 
} 

/번들/자원/설정/services.yml 공급 업체에서 서비스를 등록

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

사용법 :

{{ form_widget(foobar)|htmldecode }} 

내가 아직 모르는 이스케이프가 수행되고 , 그것은 데이터 자체에 대해서만 수행되므로 (그리고 폼의 데이터를 수정하기 위해 데이터 이벤트를 만들려고 시도 했음), 적어도 이것이 내가 원하는 최종 결과를 제공합니다.

+0

이 코드를 시도하고 나뭇 가지 확장을 등록했지만' form_label'. 당신은'form_widget'에 그것을 적용해야합니다. – Dean

3

은 당신이 정말 일을해야하는 것입니다.

관련 문제