Ver Fonte

[Form] Removed notion of "hidden" fields

Instead, hidden fields now override the "row" template to not include a label or errors.

The "rest" (former "hidden") helper has been adapted to output any fields that were not
rendered manually. It should usually be called at the end of a form.
Bernhard Schussek há 14 anos atrás
pai
commit
c1edf116f2

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

@@ -23,8 +23,8 @@
 {% block rest %}
 {% spaceless %}
     {% for field in fields %}
-        {% if field.vars.hidden %}
-            {{ field.widget }}
+        {% if not field.rendered %}
+            {{ field.row }}
         {% endif %}
     {% endfor %}
 {% endspaceless %}
@@ -52,7 +52,7 @@
 {% block form__widget %}
 {% spaceless %}
     {{ this.errors }}
-    {% for field in visible_fields %}
+    {% for field in fields %}
         {{ field.row }}
     {% endfor %}
     {{ this.rest }}
@@ -84,6 +84,12 @@
 {% endspaceless %}
 {% endblock hidden__widget %}
 
+{% block hidden__row %}
+{% spaceless %}
+    {{ this.widget }}
+{% endspaceless %}
+{% endblock hidden__row %}
+
 {% block textarea__widget %}
 {% spaceless %}
     <textarea {{ block('attributes') }}>{{ value }}</textarea>

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

@@ -187,23 +187,6 @@ class Field extends Configurable implements FieldInterface
         return false;
     }
 
-    public function setHidden($hidden)
-    {
-        $this->hidden = $hidden;
-
-        return $this;
-    }
-
-    /**
-     * Returns true if the widget is hidden.
-     *
-     * @return Boolean true if the widget is hidden, false otherwise
-     */
-    public function isHidden()
-    {
-        return $this->hidden;
-    }
-
     /**
      * {@inheritDoc}
      */

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

@@ -176,13 +176,6 @@ interface FieldInterface
      */
     function isDisabled();
 
-    /**
-     * Returns whether the field is hidden
-     *
-     * @return Boolean
-     */
-    function isHidden();
-
     /**
      * Returns whether the field is empty
      *

+ 2 - 2
src/Symfony/Component/Form/Form.php

@@ -460,7 +460,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface
                     $pathIterator->next();
                 }
 
-                if ($this->has($pathIterator->current()) && !$this->get($pathIterator->current())->isHidden()) {
+                if ($this->has($pathIterator->current())) {
                     $this->get($pathIterator->current())->addError($error, $pathIterator);
 
                     return;
@@ -471,7 +471,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface
 
                 foreach ($iterator as $field) {
                     if (null !== ($fieldPath = $field->getPropertyPath())) {
-                        if ($fieldPath->getElement(0) === $pathIterator->current() && !$field->isHidden()) {
+                        if ($fieldPath->getElement(0) === $pathIterator->current()) {
                             if ($pathIterator->hasNext()) {
                                 $pathIterator->next();
                             }

+ 1 - 2
src/Symfony/Component/Form/FormFactory.php

@@ -225,8 +225,7 @@ class FormFactory
             'template' => 'hidden',
         ), $options);
 
-        return $this->getField($key, $options)
-            ->setHidden(true);
+        return $this->getField($key, $options);
     }
 
     public function getNumberField($key, array $options = array())

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

@@ -29,6 +29,8 @@ class DefaultRenderer implements RendererInterface
 
     private $initialized = false;
 
+    private $rendered = false;
+
     public function __construct(ThemeInterface $theme, $template)
     {
         $this->theme = $theme;
@@ -95,8 +97,15 @@ class DefaultRenderer implements RendererInterface
         return $this->vars;
     }
 
+    public function isRendered()
+    {
+        return $this->rendered;
+    }
+
     public function getWidget(array $vars = array())
     {
+        $this->rendered = true;
+
         return $this->render('widget', $vars);
     }
 

+ 0 - 1
src/Symfony/Component/Form/Renderer/Plugin/FieldPlugin.php

@@ -52,7 +52,6 @@ class FieldPlugin implements PluginInterface
         $renderer->setVar('name', $name);
         $renderer->setVar('errors', $this->field->getErrors());
         $renderer->setVar('value', $this->field->getDisplayedData());
-        $renderer->setVar('hidden', $this->field->isHidden());
         $renderer->setVar('disabled', $this->field->isDisabled());
         $renderer->setVar('required', $this->field->isRequired());
         $renderer->setVar('class', null);

+ 0 - 6
src/Symfony/Component/Form/Renderer/Plugin/FormPlugin.php

@@ -26,17 +26,11 @@ class FormPlugin implements PluginInterface
     public function setUp(RendererInterface $renderer)
     {
         $fields = array();
-        $visibleFields = array();
 
         foreach ($this->form as $key => $field) {
             $fields[$key] = $field->getRenderer();
-
-            if (!$field->isHidden()) {
-                $visibleFields[$key] = $field->getRenderer();
-            }
         }
 
         $renderer->setVar('fields', $fields);
-        $renderer->setVar('visible_fields', $visibleFields);
     }
 }

+ 0 - 32
tests/Symfony/Tests/Component/Form/HiddenFieldTest.php

@@ -1,32 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Tests\Component\Form;
-
-require_once __DIR__.'/TestCase.php';
-
-use Symfony\Component\Form\HiddenField;
-
-class HiddenFieldTest extends TestCase
-{
-    protected $field;
-
-    protected function setUp()
-    {
-        parent::setUp();
-        $this->field = $this->factory->getHiddenField('name');
-    }
-
-    public function testIsHidden()
-    {
-        $this->assertTrue($this->field->isHidden());
-    }
-}