Browse Source

[Form] Refactored id and name generation to renderer plugins

Bernhard Schussek 14 years ago
parent
commit
f7dc71ef59

+ 4 - 4
src/Symfony/Bundle/TwigBundle/Resources/views/form.html.twig

@@ -30,7 +30,7 @@
 
 {% block label %}
 {% spaceless %}
-    <label for="{{ field.id }}">{% trans label %}</label>
+    <label for="{{ id }}">{% trans label %}</label>
 {% endspaceless %}
 {% endblock label %}
 
@@ -44,7 +44,7 @@
 
 {% block field_attributes %}
 {% spaceless %}
-    id="{{ field.id }}" name="{{ field.name }}"{% if field.disabled %} disabled="disabled"{% endif %}{% if field.required %} required="required"{% endif %}
+    id="{{ id }}" name="{{ name }}"{% if field.disabled %} disabled="disabled"{% endif %}{% if field.required %} required="required"{% endif %}
     {{ block('attributes') }}
 {% endspaceless %}
 {% endblock field_attributes %}
@@ -85,7 +85,7 @@
 
 {% block hidden__widget %}
 {% spaceless %}
-    <input type="hidden" id="{{ field.id }}" name="{{ field.name }}"{% if field.disabled %} disabled="disabled"{% endif %} value="{{ field.displayedData }}" />
+    <input type="hidden" id="{{ id }}" name="{{ name }}"{% if field.disabled %} disabled="disabled"{% endif %} value="{{ field.displayedData }}" />
 {% endspaceless %}
 {% endblock hidden__widget %}
 
@@ -116,7 +116,7 @@
     {% if field.isExpanded %}
         {% for choice, child in field %}
             {{ child.renderer.widget }}
-            <label for="{{ child.id }}">{{ field.label(choice) }}</label>
+            <label for="{{ id }}">{{ field.label(choice) }}</label>
         {% endfor %}
     {% else %}
         <select {{ block('field_attributes') }}{% if field.isMultipleChoice %} multiple="multiple"{% endif %}>

+ 0 - 16
src/Symfony/Component/Form/Field.php

@@ -165,22 +165,6 @@ class Field extends Configurable implements FieldInterface
         return $this->key;
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public function getName()
-    {
-        return null === $this->parent ? $this->key : $this->parent->getName().'['.$this->key.']';
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getId()
-    {
-        return null === $this->parent ? $this->key : $this->parent->getId().'_'.$this->key;
-    }
-
     /**
      * {@inheritDoc}
      */

+ 0 - 21
src/Symfony/Component/Form/FieldInterface.php

@@ -69,27 +69,6 @@ interface FieldInterface
      */
     function getKey();
 
-    /**
-     * Returns the name of the field.
-     *
-     * @return string  When the field has no parent, the name is equal to its
-     *                 key. If the field has a parent, the name is composed of
-     *                 the parent's name and the field's key, where the field's
-     *                 key is wrapped in squared brackets
-     *                 (e.g. "parent_name[field_key]")
-     */
-    function getName();
-
-    /**
-     * Returns the ID of the field.
-     *
-     * @return string  The ID of a field is equal to its name, where all
-     *                 sequences of squared brackets are replaced by a single
-     *                 underscore (e.g. if the name is "parent_name[field_key]",
-     *                 the ID is "parent_name_field_key").
-     */
-    function getId();
-
     /**
      * Sets the property path
      *

+ 17 - 1
src/Symfony/Component/Form/Form.php

@@ -734,7 +734,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface
      */
     public function bind(Request $request, $data = null)
     {
-        if (!$this->getName()) {
+        if (!$this->getKey()) {
             throw new FormException('You cannot bind anonymous forms. Please give this form a name');
         }
 
@@ -755,6 +755,22 @@ class Form extends Field implements \IteratorAggregate, FormInterface
         }
     }
 
+    /**
+     * @deprecated
+     */
+    private function getName()
+    {
+        return null === $this->getParent() ? $this->getKey() : $this->getParent()->getName().'['.$this->key.']';
+    }
+
+    /**
+     * @deprecated
+     */
+    private function getId()
+    {
+        return null === $this->getParent() ? $this->getKey() : $this->getParent()->getId().'_'.$this->key;
+    }
+
     /**
      * Validates the form and its domain object
      *

+ 8 - 0
src/Symfony/Component/Form/Renderer/DefaultRenderer.php

@@ -67,6 +67,14 @@ class DefaultRenderer implements RendererInterface
         $this->parameters[$name] = $value;
     }
 
+    public function getParameter($name)
+    {
+        $this->setUpPlugins();
+
+        // TODO exception handling
+        return $this->parameters[$name];
+    }
+
     public function getWidget(array $attributes = array(), array $parameters = array())
     {
         return $this->render('widget', $attributes, $parameters);

+ 49 - 0
src/Symfony/Component/Form/Renderer/Plugin/IdPlugin.php

@@ -0,0 +1,49 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license infieldation, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Renderer\Plugin;
+
+use Symfony\Component\Form\Renderer\RendererInterface;
+use Symfony\Component\Form\FieldInterface;
+
+class IdPlugin implements PluginInterface
+{
+    private $field;
+
+    public function __construct(FieldInterface $field)
+    {
+        $this->field = $field;
+    }
+
+    /**
+     * Renders the HTML enctype in the field tag, if necessary
+     *
+     * Example usage in Twig templates:
+     *
+     *     <field action="..." method="post" {{ field.render.enctype }}>
+     *
+     * @param Form $field   The field for which to render the encoding type
+     */
+    public function setUp(RendererInterface $renderer)
+    {
+        $fieldKey = $this->field->getKey();
+
+        if ($this->field->hasParent()) {
+            $parentRenderer = $this->field->getParent()->getRenderer();
+            $parentId = $parentRenderer->getParameter('id');
+            $id = sprintf('%s_%s', $parentId, $fieldKey);
+        } else {
+            $id = $fieldKey;
+        }
+
+        $renderer->setParameter('id', $id);
+    }
+}

+ 49 - 0
src/Symfony/Component/Form/Renderer/Plugin/NamePlugin.php

@@ -0,0 +1,49 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license infieldation, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Renderer\Plugin;
+
+use Symfony\Component\Form\Renderer\RendererInterface;
+use Symfony\Component\Form\FieldInterface;
+
+class NamePlugin implements PluginInterface
+{
+    private $field;
+
+    public function __construct(FieldInterface $field)
+    {
+        $this->field = $field;
+    }
+
+    /**
+     * Renders the HTML enctype in the field tag, if necessary
+     *
+     * Example usage in Twig templates:
+     *
+     *     <field action="..." method="post" {{ field.render.enctype }}>
+     *
+     * @param Form $field   The field for which to render the encoding type
+     */
+    public function setUp(RendererInterface $renderer)
+    {
+        $fieldKey = $this->field->getKey();
+
+        if ($this->field->hasParent()) {
+            $parentRenderer = $this->field->getParent()->getRenderer();
+            $parentName = $parentRenderer->getParameter('name');
+            $name = sprintf('%s[%s]', $parentName, $fieldKey);
+        } else {
+            $name = $fieldKey;
+        }
+
+        $renderer->setParameter('name', $name);
+    }
+}

+ 0 - 24
tests/Symfony/Tests/Component/Form/FieldTest.php

@@ -121,30 +121,6 @@ class FieldTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($this->field->isValid());
     }
 
-    public function testGetNameReturnsKey()
-    {
-        $this->assertEquals('title', $this->field->getName());
-    }
-
-    public function testGetNameIncludesParent()
-    {
-        $this->field->setParent($this->createMockGroupWithName('news[article]'));
-
-        $this->assertEquals('news[article][title]', $this->field->getName());
-    }
-
-    public function testGetIdReturnsKey()
-    {
-        $this->assertEquals('title', $this->field->getId());
-    }
-
-    public function testGetIdIncludesParent()
-    {
-        $this->field->setParent($this->createMockGroupWithId('news_article'));
-
-        $this->assertEquals('news_article_title', $this->field->getId());
-    }
-
     public function testIsRequiredReturnsOwnValueIfNoParent()
     {
         $this->field->setRequired(true);