Преглед изворни кода

[Form][TwigBundle] Making sure all field types are rendered with the proper template

Jordi Boggiano пре 14 година
родитељ
комит
d45954af07

+ 9 - 6
src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php

@@ -6,6 +6,7 @@ use Symfony\Component\Form\Form;
 use Symfony\Component\Form\FieldGroupInterface;
 use Symfony\Component\Form\FieldInterface;
 use Symfony\Component\Form\CollectionField;
+use Symfony\Component\Form\HybridField;
 use Symfony\Bundle\TwigBundle\TokenParser\FormThemeTokenParser;
 use Symfony\Bundle\FrameworkBundle\Templating\HtmlGeneratorInterface;
 
@@ -96,16 +97,18 @@ class FormExtension extends \Twig_Extension
             $this->templates = $this->resolveResources($this->resources);
         }
 
-        if ($field instanceof FieldGroupInterface) {
-            return $this->templates['group']->getBlock('group', array(
-                'group'      => $field,
+        if ($field instanceof CollectionField) {
+            return $this->templates['group']->getBlock('collection', array(
+                'collection' => $field,
                 'attributes' => $attributes,
             ));
         }
 
-        if ($field instanceof CollectionField) {
-            return $this->templates['group']->getBlock('collection', array(
-                'collection' => $field,
+        // FieldGroupInterface instances that need to be rendered as
+        // a field instead of group should return false in isGroup()
+        if ($field instanceof FieldGroupInterface && true === $field->isGroup()) {
+            return $this->templates['group']->getBlock('group', array(
+                'group'      => $field,
                 'attributes' => $attributes,
             ));
         }

+ 5 - 0
src/Symfony/Component/Form/FieldGroup.php

@@ -447,6 +447,11 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
         return false;
     }
 
+    public function isGroup()
+    {
+        return true;
+    }
+
     /**
      * Returns true if the bound field exists (implements the \ArrayAccess interface).
      *

+ 5 - 0
src/Symfony/Component/Form/FieldGroupInterface.php

@@ -18,4 +18,9 @@ namespace Symfony\Component\Form;
  */
 interface FieldGroupInterface extends FieldInterface, \ArrayAccess, \Traversable, \Countable
 {
+    /**
+     * Returns whether the Field instance really is a group
+     * @return bool
+     */
+    public function isGroup();
 }