2011-09-17 11 views

Respuesta

1

De forma predeterminada, Twig utiliza un diseño div al representar formularios. Sin embargo, puede representar formularios en un diseño de tabla. Utilizar el recurso form_table_layout.html.twig usar un diseño de este tipo:

# app/config/config.yml 

twig: 
    form: 
     resources: ['form_table_layout.html.twig'] 
3

Por el momento, he implementado soporte de campos así:

en el Tipo:

public function buildView(FormView $view, FormInterface $form, array $options) 
{ 
    $view->setAttribute('fieldsets', 
      array(
       array(
        'legend' => 'film.group.date', 
        'content'=> array(
         'theaters_release_date', 
         'storage_media_release', 
         'storage_media_release_date', 
         'vod_release_date' 
         )), 
       array(
        'legend' => 'film.group.country', 
        'content'=> array('countries')), 
        )); 
} 

tengo una plantilla llamada fieldset.html.twig, que utiliza los atributos de la vista:

{% macro fieldset_block(fieldset, form) %} 
<fieldset{% if fieldset.subform is defined %} class="{{ fieldset.subform }}"{% endif %}> 
    <legend>{{fieldset.legend | trans }}</legend> 
    {% if fieldset.content is defined%} 
     {% for row in fieldset.content %} 
      {{ form_row(form[row]) }} 
     {% endfor %} 
    {% endif %} 
    {% if fieldset.subform is defined %} 
     {# Couldn't get some recursivity (simply call form widget) here... too bad #} 
     {% if form[fieldset.subform].get('attr').fieldsets is defined %} 
      {% for subfieldset in form[fieldset.subform].get('attr').fieldsets %} 
       {{ _self.fieldset_block(subfieldset, form[fieldset.subform]) }} 
      {% endfor %} 
     {% else %} 
      {% for row in form[fieldset.subform] %} 
       {{ form_row(row) }} 
      {% endfor %} 
     {% endif %} 
    {% endif %} 
    {% if fieldset.items is defined%} 
     {% for fieldset in fieldset.items %} 
      {{ _self.fieldset_block(fieldset, form) }} 
     {% endfor %} 
    {% endif %} 
</fieldset> 
{% endmacro %} 

{% block form_widget %} 
    {% for fieldset in form.get('attr').fieldsets %} 
     {{ _self.fieldset_block(fieldset, form) }} 
    {% endfor %} 
{% endblock %} 
2

Aquí es un simp Ejemplo de le fieldset: https://gist.github.com/spcmky/8512371

Para reemplazar los divs por una lista, consulte form_widget_compound y form_rows. Usted puede:

{% block fieldset_widget %} 
    {% spaceless %} 
     <fieldset {{ block('widget_container_attributes') }}> 
      {% if title is defined %}<legend>{{ title }}</legend>{% endif %} 
       <ul> 
       {% for child in form %} 
        <li> 
         {{ form_widget(child) }} 
        </li> 
       {% endfor %} 
       </ul> 
     </fieldset> 
    {% endspaceless %} 
{% endblock %} 
Cuestiones relacionadas