2012-10-02 17 views
5

Come impostare l'attributo nome del campo di rendering in symfony 2?Impostazione attributo nome del campo modulo symfony 2

risultato atteso:

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

Rendering campo in questo modo

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

sicuramente non funziona.

uscita è ancora

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

C'è un modo per impostare l'attributo id nome o attributi in modo dinamico? Grazie.

+1

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

risposta

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

ha già test come nome. Naturalmente c'è ancora il nome del modulo radice chiamato form nel tuo caso. Rimozione di questo non è davvero raccomandato, perché quando si legge la richiesta per popolare i dati del modulo è possibile identificare il modulo con il suo nome del modulo.

Leggi [modulo] Attiva principale vuoto nome del modulohttps://github.com/symfony/symfony/pull/2936

1

sovrascrivendo blocco Twig in questo modo:

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

Il cambiamento è solo muovendo name = {{full_name}} dalla prima linea a l'ultimo, quindi quando si aggiunge il nome attr all'elemento di aggiunta del generatore di moduli, il nome non verrà più ignorato.

+0

Per impostazione predefinita è il file "Symfony/bridge/Twig/Resources/view/form/form_div_layout.html.twig" – HRoux

Problemi correlati