Explorar o código

[Form] Changed collection prototype rendering.

Based on PR 1500. It is now rendered inside an attribute of collection
tag.
marc.weistroff %!s(int64=14) %!d(string=hai) anos
pai
achega
2b4cc9bd06

+ 1 - 1
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -223,7 +223,7 @@ class FormExtension extends \Twig_Extension
             }
         }
 
-        $custom = '_'.$view->get('proto_id', $view->get('id'));
+        $custom = '_'.$view->get('id');
         $rendering = $custom.$section;
         $blocks = $this->getBlocks($view);
 

+ 9 - 6
src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

@@ -9,6 +9,15 @@
 {% endspaceless %}
 {% endblock form_widget %}
 
+{% block collection_widget %}
+{% spaceless %}
+    {% if prototype is defined %}
+        {% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
+    {% endif %}
+    {{ block('form_widget') }}
+{% endspaceless %}
+{% endblock collection_widget %}
+
 {% block textarea_widget %}
 {% spaceless %}
     <textarea {{ block('widget_attributes') }}>{{ value }}</textarea>
@@ -222,12 +231,6 @@
 {% endspaceless %}
 {% endblock field_row %}
 
-{% block prototype_row %}
-{% spaceless %}
-    <script type="text/html" id="{{ proto_id }}">{{ form_row(form) }}</script>
-{% endspaceless %}
-{% endblock prototype_row %}
-
 {% block hidden_row %}
     {{ form_widget(form) }}
 {% endblock hidden_row %}

+ 4 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/collection_widget.html.php

@@ -0,0 +1,4 @@
+<?php if (isset($prototype)): ?>
+    <?php $attr['data-prototype'] = $view->escape($view['form']->row($prototype)) ?>
+<?php endif ?>
+<?php echo $view['form']->widget($form, array('attr' => $attr)) ?>

+ 0 - 3
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/prototype_row.html.php

@@ -1,3 +0,0 @@
-<script type="text/html" id="<?php echo $view->escape($proto_id) ?>">
-    <?php echo $view['form']->row($form) ?>
-</script>

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

@@ -199,7 +199,7 @@ class FormHelper extends Helper
 
         $template = null;
 
-        $custom = '_'.$view->get('proto_id', $view->get('id'));
+        $custom = '_'.$view->get('id');
         $rendering = $custom.$section;
 
         if (isset($this->varStack[$rendering])) {

+ 3 - 2
src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php

@@ -27,7 +27,8 @@ class CollectionType extends AbstractType
     {
         if ($options['allow_add'] && $options['prototype']) {
             $prototype = $builder->create('$$name$$', $options['type'], $options['options']);
-            $builder->setAttribute('prototype', $prototype);
+            $builder->setAttribute('prototype', $prototype->getForm());
+            $builder->add($prototype);
         }
 
         $dataClass = isset($options['options']['data_class']) ? $options['options']['data_class'] : null;
@@ -63,7 +64,7 @@ class CollectionType extends AbstractType
         ;
 
         if ($form->hasAttribute('prototype')) {
-            $view->set('prototype', $form->getAttribute('prototype'));
+            $view->set('prototype', $form->getAttribute('prototype')->createView($view));
         }
     }
 

+ 0 - 10
src/Symfony/Component/Form/Form.php

@@ -925,16 +925,6 @@ class Form implements \IteratorAggregate, FormInterface
             $childViews[$key] = $child->createView($view);
         }
 
-        if (null !== $prototype = $view->get('prototype')) {
-            $protoView = $prototype->getForm()->createView($view);
-            $protoTypes = $protoView->get('types');
-            $protoTypes[] = 'prototype';
-            $childViews[$prototype->getName()] = $protoView
-                ->set('types', $protoTypes)
-                ->set('proto_id', $view->get('id').'_prototype');
-            ;
-        }
-
         $view->setChildren($childViews);
 
         foreach ($types as $type) {

+ 4 - 3
tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

@@ -1692,9 +1692,10 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         $html = $this->renderWidget($form);
 
         $this->assertMatchesXpath($html,
-'//script
-    [@id="na&me_items_prototype"]
-    [@type="text/html"]
+            '//div[@id="na&me_items"][@data-prototype]
+            |
+             //table[@id="na&me_items"][@data-prototype]
+
 '
         );
     }