Browse Source

[Form] Fix collection rendering

Victor Berchet 14 năm trước cách đây
mục cha
commit
bca17fe6a3

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/collection_label.html.php

@@ -0,0 +1 @@
+<label <?php echo $view['form']->attributes() ?>><?php echo $view->escape($view['translator']->trans($label)) ?></label>

+ 0 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/collection_row.html.php

@@ -1 +0,0 @@
-<?php echo $view['form']->render('form', 'widget', $renderer->all()); ?>

+ 8 - 2
src/Symfony/Bundle/TwigBundle/Resources/views/Form/div_layout.html.twig

@@ -41,6 +41,12 @@
 {% endspaceless %}
 {% endspaceless %}
 {% endblock field_label %}
 {% endblock field_label %}
 
 
+{% block collection_label %}
+{% spaceless %}
+    <label {% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }}</label>
+{% endspaceless %}
+{% endblock collection_label %}
+
 {% block attributes %}
 {% block attributes %}
 {% spaceless %}
 {% spaceless %}
     id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
     id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
@@ -50,11 +56,11 @@
 
 
 {% block container_attributes %}
 {% block container_attributes %}
 {% spaceless %}
 {% spaceless %}
-    id="{{ id }}" 
+    id="{{ id }}"
     {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}
     {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}
 {% endspaceless %}
 {% endspaceless %}
 {% endblock container_attributes %}
 {% endblock container_attributes %}
-    
+
 {% block field_widget %}
 {% block field_widget %}
 {% spaceless %}
 {% spaceless %}
     {% set type = type|default('text') %}
     {% set type = type|default('text') %}

+ 40 - 0
tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php

@@ -133,6 +133,46 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
         );
         );
     }
     }
 
 
+    public function testCollectionRow()
+    {
+        $collection = $this->factory->createNamedBuilder(
+            'collection',
+            'collection',
+            array('a', 'b'),
+            array('type' => 'text')
+        );
+
+        $form = $this->factory->createNamedBuilder('form', 'form')
+          ->add($collection)
+          ->getForm();
+
+        $this->assertWidgetMatchesXpath($form->createView(), array(),
+'/div
+    [
+        ./input[@type="hidden"][@id="form__token"]
+        /following-sibling::div
+            [
+                ./label[not(@for)]
+                /following-sibling::div
+                    [
+                        ./div
+                            [
+                                ./label[@for="form_collection_0"]
+                                /following-sibling::input[@type="text"][@value="a"]
+                            ]
+                        /following-sibling::div
+                            [
+                                ./label[@for="form_collection_1"]
+                                /following-sibling::input[@type="text"][@value="b"]
+                            ]
+                    ]
+            ]
+    ]
+    [count(.//input)=3]
+'
+        );
+    }
+
     public function testForm()
     public function testForm()
     {
     {
         $form = $this->factory->createNamedBuilder('form', 'name')
         $form = $this->factory->createNamedBuilder('form', 'name')