2012-10-02 3 views
5

심포니 2에서 렌더링 된 필드의 이름 속성을 설정하는 방법은 무엇입니까?심포니 2 폼 필드의 이름 속성 설정

예상 출력 : 현장을

 {{ form_widget(form.test, { 'attr': {'name': 'test'} }) }} 

확실히 작동하지 않습니다 이런 식으로 렌더링

 <input type="text" name="test" value="test" /> 

.

출력

 <input type="text" name="form[test]" value="test" />. 

이름 속성이나 ID가 동적으로 속성을 설정할 수있는 방법이 있나요 아직? 고맙습니다.

+1

참조 http://stackoverflow.com/questions/8416783/symfony2-form-component-creating-fields-without-the-forms-name-in-the-name-att – max

답변

1
<input type="text" name="form[test]" value="test" /> 

이미 이름이 test입니다. 물론 form이라는 루트 폼 이름이 여전히 있습니다. 폼 데이터를 채우라는 요청을 읽을 때 폼 이름으로 폼을 식별 할 수 있기 때문에 이것을 제거하는 것은 실제로 권장되지 않습니다.

{% block widget_attributes -%} 
id="{{ id }}" 
{%- if read_only %} readonly="readonly"{% endif -%} 
{%- if disabled %} disabled="disabled"{% endif -%} 
{%- if required %} required="required"{% endif -%} 
{%- for attrname, attrvalue in attr -%} 
    {{- " " -}} 
    {%- if attrname in ['placeholder', 'title'] -%} 
     {{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}" 
    {%- elseif attrvalue is sameas(true) -%} 
     {{- attrname }}="{{ attrname }}" 
    {%- elseif attrvalue is not sameas(false) -%} 
     {{- attrname }}="{{ attrvalue }}" 
    {%- endif -%} 
{%- endfor -%} 
name={{full_name}} 
{%- endblock widget_attributes %} 

변화는 단지 이동 NAME = {{FULL_NAME}} 첫 번째 줄 내지 :

읽기 [형태] 나뭇 블록이 방법을 무시 빈 루트 폼 이름https://github.com/symfony/symfony/pull/2936

1

사용 마지막으로 form builder add 요소에 attr이라는 이름을 추가하면 이름이 더 이상 무시되지 않습니다.

+0

기본적으로 "Symfony/Bridge/Twig/Resources/view/Form/form_div_layout.html.twig " – HRoux