Ver Fonte

Merge pull request #3127 from vincenttouzet/master

Fix #2630 : Checkbox labels NOT showing with Bootstrap SF2.6 form theme
Oskar Stark há 9 anos atrás
pai
commit
67c55b3f4b

+ 75 - 26
Resources/views/Form/form_admin_fields.html.twig

@@ -78,6 +78,28 @@ file that was distributed with this source code.
     {% endspaceless %}
 {% endblock percent_widget %}
 
+{% block checkbox_widget -%}
+    {% set parent_label_class = parent_label_class|default('') -%}
+    {% if 'checkbox-inline' in parent_label_class or sonata_admin.options['use_icheck'] %}
+        {{- form_label(form, null, { widget: parent() }) -}}
+    {% else -%}
+        <div class="checkbox">
+            {{- form_label(form, null, { widget: parent() }) -}}
+        </div>
+    {%- endif %}
+{%- endblock checkbox_widget %}
+
+{% block radio_widget -%}
+    {%- set parent_label_class = parent_label_class|default('') -%}
+    {% if 'radio-inline' in parent_label_class or sonata_admin.options['use_icheck'] %}
+        {{- form_label(form, null, { widget: parent() }) -}}
+    {% else -%}
+        <div class="radio">
+            {{- form_label(form, null, { widget: parent() }) -}}
+        </div>
+    {%- endif %}
+{%- endblock radio_widget %}
+
 {# Labels #}
 {% block form_label %}
 {% spaceless %}
@@ -108,40 +130,54 @@ file that was distributed with this source code.
             {%- endif -%}
         {% endif %}
 
-        {% if in_list_checkbox is defined and in_list_checkbox and widget is defined %}
-            <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
-                {{ widget|raw }}
-                <span>
-                    {% if not sonata_admin.admin %}
-                        {{- label|trans({}, translation_domain) -}}
-                    {% else %}
-                        {{- label|trans({}, sonata_admin.field_description.translationDomain) -}}
-                    {% endif%}
-                </span>
-            </label>
-        {% else %}
-            <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
-                {% if not sonata_admin.admin%}
-                    {{- label|trans({}, translation_domain) -}}
-                {% else %}
-                    {{ sonata_admin.admin.trans(label, {}, sonata_admin.field_description.translationDomain) }}
-                {% endif %}
-            </label>
-        {% endif %}
+        <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
+            {% if not sonata_admin.admin %}
+                {{- label|trans({}, translation_domain) -}}
+            {% else %}
+                {{ sonata_admin.admin.trans(label, {}, sonata_admin.field_description.translationDomain) }}
+            {% endif %}
+        </label>
     {% endif %}
 {% endspaceless %}
 {% endblock form_label %}
 
+{% block checkbox_label -%}
+    {{- block('checkbox_radio_label') -}}
+{%- endblock checkbox_label %}
+
+{% block radio_label -%}
+    {{- block('checkbox_radio_label') -}}
+{%- endblock radio_label %}
+
+{% block checkbox_radio_label %}
+    {% if sonata_admin.admin %}
+        {% set translation_domain = sonata_admin.field_description.translationDomain %}
+    {% endif %}
+    {# Do not display the label if widget is not defined in order to prevent double label rendering #}
+    {% if widget is defined %}
+        {% if required %}
+            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
+        {% endif %}
+        {% if parent_label_class is defined %}
+            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ parent_label_class)|trim}) %}
+        {% endif %}
+        {% if label is not same as(false) and label is empty %}
+            {% set label = name|humanize %}
+        {% endif %}
+        <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
+            {{- widget|raw -}}
+            {{- label is not same as(false) ? label|trans({}, translation_domain) -}}
+        </label>
+    {% endif %}
+{% endblock checkbox_radio_label %}
+
 {% block choice_widget_expanded %}
 {% spaceless %}
     {% set attr = attr|merge({'class': attr.class|default('') ~ ' list-unstyled'}) %}
     <ul {{ block('widget_container_attributes') }}>
     {% for child in form %}
         <li>
-            {% set form_widget_content %}
-                {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
-            {% endset %}
-            {{ form_label(child, child.vars.label|default(null), { 'in_list_checkbox' : true, 'widget' : form_widget_content } ) }}
+            {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
         </li>
     {% endfor %}
     </ul>
@@ -257,6 +293,7 @@ file that was distributed with this source code.
 {% endblock datetime_widget %}
 
 {% block form_row %}
+    {% set show_label = show_label|default(true) %}
     <div class="form-group{% if errors|length > 0 %} has-error{% endif %}" id="sonata-ba-field-container-{{ id }}">
         {% if sonata_admin.field_description.options is defined %}
             {% set label = sonata_admin.field_description.options.name|default(label)  %}
@@ -268,7 +305,7 @@ file that was distributed with this source code.
             {% set div_class = div_class ~ ' sonata-collection-row-without-label' %}
         {% endif %}
 
-        {% if sonata_admin.options['form_type'] == 'horizontal' %}
+        {% if sonata_admin is defined and sonata_admin.options['form_type'] == 'horizontal' %}
             {% if label is same as(false) %}
                 {% if 'collection' in form.parent.vars.block_prefixes %}
                     {% set div_class = div_class ~ ' col-sm-12' %}
@@ -280,7 +317,9 @@ file that was distributed with this source code.
             {% endif %}
         {% endif %}
 
-        {{ form_label(form, label|default(null)) }}
+        {% if show_label %}
+            {{ form_label(form, label|default(null)) }}
+        {% endif %}
 
         {% if sonata_admin is defined and sonata_admin_enabled %}
             {% set div_class = div_class ~ ' sonata-ba-field-' ~ sonata_admin.edit ~ '-' ~ sonata_admin.inline %}
@@ -306,6 +345,16 @@ file that was distributed with this source code.
     </div>
 {% endblock form_row %}
 
+{% block checkbox_row -%}
+    {% set show_label = false %}
+    {{ block('form_row') }}
+{%- endblock checkbox_row %}
+
+{% block radio_row -%}
+    {% set show_label = false %}
+    {{ block('form_row') }}
+{%- endblock radio_row %}
+
 {% block sonata_type_native_collection_widget_row %}
 {% spaceless %}
     <div class="sonata-collection-row">

+ 28 - 17
Tests/Form/Widget/BaseWidgetTest.php

@@ -35,6 +35,11 @@ abstract class BaseWidgetTest extends TypeTestCase
      */
     protected $extension;
 
+    /**
+     * @var \Twig_Environment
+     */
+    protected $environment;
+
     /**
      * Current template type, form or filter.
      *
@@ -42,6 +47,23 @@ abstract class BaseWidgetTest extends TypeTestCase
      */
     protected $type = null;
 
+    /**
+     * @var array
+     */
+    protected $sonataAdmin = array(
+        'name'              => null,
+        'admin'             => null,
+        'value'             => null,
+        'edit'              => 'standard',
+        'inline'            => 'natural',
+        'field_description' => null,
+        'block_name'        => false,
+        'options'           => array(
+            'form_type'  => 'vertical',
+            'use_icheck' => true,
+        ),
+    );
+
     /**
      * {@inheritdoc}
      */
@@ -81,29 +103,18 @@ abstract class BaseWidgetTest extends TypeTestCase
 
         $loader = new StubFilesystemLoader($twigPaths);
 
-        $environment = new \Twig_Environment($loader, array('strict_variables' => true));
-        $environment->addGlobal('sonata_admin', $this->getSonataAdmin());
-        $environment->addExtension(new TranslationExtension(new StubTranslator()));
+        $this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
+        $this->environment->addGlobal('sonata_admin', $this->getSonataAdmin());
+        $this->environment->addExtension(new TranslationExtension(new StubTranslator()));
 
-        $environment->addExtension($this->extension);
+        $this->environment->addExtension($this->extension);
 
-        $this->extension->initRuntime($environment);
+        $this->extension->initRuntime($this->environment);
     }
 
     protected function getSonataAdmin()
     {
-        return array(
-            'name'              => null,
-            'admin'             => null,
-            'value'             => null,
-            'edit'              => 'standard',
-            'inline'            => 'natural',
-            'field_description' => null,
-            'block_name'        => false,
-            'options'           => array(
-                'form_type' => 'vertical',
-            ),
-        );
+        return $this->sonataAdmin;
     }
 
     /**

+ 29 - 1
Tests/Form/Widget/FormChoiceWidgetTest.php

@@ -41,7 +41,35 @@ class FormChoiceWidgetTest extends BaseWidgetTest
         $html = $this->renderWidget($choice->createView());
 
         $this->assertContains(
-            '<span>[trans]some[/trans]</span>',
+            '<li><label><input type="checkbox" id="choice_0" name="choice[]" value="0" />[trans]some[/trans]</label></li>',
+            $this->cleanHtmlWhitespace($html)
+        );
+    }
+
+    public function testBootstrapLabelRendering()
+    {
+        $sonataAdmin = $this->getSonataAdmin();
+        $sonataAdmin['options']['use_icheck'] = false;
+        $this->environment->addGlobal('sonata_admin', $sonataAdmin);
+
+        $choices = array('some', 'choices');
+        if (!method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions')) {
+            $choices = array_flip($choices);
+        }
+
+        $choice = $this->factory->create(
+            $this->getChoiceClass(),
+            null,
+            $this->getDefaultOption() + array(
+                'multiple' => true,
+                'expanded' => true,
+            ) + compact('choices')
+        );
+
+        $html = $this->renderWidget($choice->createView());
+
+        $this->assertContains(
+            '<li><div class="checkbox"><label><input type="checkbox" id="choice_0" name="choice[]" value="0" />[trans]some[/trans]</label></div></li>',
             $this->cleanHtmlWhitespace($html)
         );
     }