Explorar o código

[Form] Renamed 'key' to 'name'. Removed setKey() totally.

Bernhard Schussek %!s(int64=14) %!d(string=hai) anos
pai
achega
cb283d3783

+ 3 - 3
src/Symfony/Component/Form/Config/AbstractFieldConfig.php

@@ -28,16 +28,16 @@ abstract class AbstractFieldConfig implements FieldConfigInterface
         return $this->factory;
     }
 
-    protected function getInstance($identifier, $key = null, array $options = array())
+    protected function getInstance($identifier, $name = null, array $options = array())
     {
-        return $this->factory->getInstance($identifier, $key, $options);
+        return $this->factory->getInstance($identifier, $name, $options);
     }
 
     public function configure(FieldInterface $field, array $options)
     {
     }
 
-    public function createInstance($key)
+    public function createInstance($name)
     {
         return null;
     }

+ 1 - 1
src/Symfony/Component/Form/Config/CollectionFieldConfig.php

@@ -19,7 +19,7 @@ class CollectionFieldConfig extends AbstractFieldConfig
     public function configure(FieldInterface $field, array $options)
     {
         if ($options['modifiable']) {
-            $field->add($options['prototype'], '$$key$$', array(
+            $field->add($options['prototype'], '$$name$$', array(
                 'property_path' => null,
                 'required' => false,
             ));

+ 4 - 4
src/Symfony/Component/Form/Config/FieldConfig.php

@@ -34,7 +34,7 @@ class FieldConfig extends AbstractFieldConfig
     public function configure(FieldInterface $field, array $options)
     {
         $field->setPropertyPath($options['property_path'] === false
-                    ? $field->getKey()
+                    ? $field->getName()
                     : $options['property_path'])
             ->setRequired($options['required'])
             ->setDisabled($options['disabled'])
@@ -46,7 +46,7 @@ class FieldConfig extends AbstractFieldConfig
             ->setRendererVar('class', null)
             ->setRendererVar('max_length', null)
             ->setRendererVar('size', null)
-            ->setRendererVar('label', ucfirst(strtolower(str_replace('_', ' ', $field->getKey()))));
+            ->setRendererVar('label', ucfirst(strtolower(str_replace('_', ' ', $field->getName()))));
 
         if ($options['trim']) {
             $field->addEventSubscriber(new TrimListener());
@@ -67,9 +67,9 @@ class FieldConfig extends AbstractFieldConfig
         );
     }
 
-    public function createInstance($key)
+    public function createInstance($name)
     {
-        return new Field($key, new EventDispatcher());
+        return new Field($name, new EventDispatcher());
     }
 
     public function getParent(array $options)

+ 1 - 1
src/Symfony/Component/Form/Config/FieldConfigInterface.php

@@ -17,7 +17,7 @@ interface FieldConfigInterface
 {
     function configure(FieldInterface $field, array $options);
 
-    function createInstance($key);
+    function createInstance($name);
 
     function getDefaultOptions(array $options);
 

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

@@ -64,9 +64,9 @@ class FormConfig extends AbstractFieldConfig
         );
     }
 
-    public function createInstance($key)
+    public function createInstance($name)
     {
-        return new Form($key, new EventDispatcher(), $this->getFormFactory(),
+        return new Form($name, new EventDispatcher(), $this->getFormFactory(),
                 $this->csrfProvider, $this->validator);
     }
 

+ 6 - 6
src/Symfony/Component/Form/Config/RepeatedFieldConfig.php

@@ -19,11 +19,11 @@ class RepeatedFieldConfig extends AbstractFieldConfig
     public function configure(FieldInterface $field, array $options)
     {
         $field->setValueTransformer(new ValueToDuplicatesTransformer(array(
-                $options['first_key'],
-                $options['second_key'],
+                $options['first_name'],
+                $options['second_name'],
             )))
-            ->add($this->getInstance($options['identifier'], $options['first_key'], $options['options']))
-            ->add($this->getInstance($options['identifier'], $options['second_key'], $options['options']));
+            ->add($this->getInstance($options['identifier'], $options['first_name'], $options['options']))
+            ->add($this->getInstance($options['identifier'], $options['second_name'], $options['options']));
     }
 
     public function getDefaultOptions(array $options)
@@ -32,8 +32,8 @@ class RepeatedFieldConfig extends AbstractFieldConfig
             'template' => 'repeated',
             'identifier' => 'text',
             'options' => array(),
-            'first_key' => 'first',
-            'second_key' => 'second',
+            'first_name' => 'first',
+            'second_name' => 'second',
             'csrf_protection' => false,
         );
     }

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

@@ -54,7 +54,7 @@ class ResizeFormListener implements EventSubscriberInterface
         }
 
         foreach ($this->form as $name => $field) {
-            if (!$this->resizeOnBind || '$$key$$' != $name) {
+            if (!$this->resizeOnBind || '$$name$$' != $name) {
                 $this->form->remove($name);
             }
         }
@@ -77,7 +77,7 @@ class ResizeFormListener implements EventSubscriberInterface
         }
 
         foreach ($this->form as $name => $field) {
-            if (!isset($data[$name]) && $this->resizeOnBind && '$$key$$' != $name) {
+            if (!isset($data[$name]) && $this->resizeOnBind && '$$name$$' != $name) {
                 $this->form->remove($name);
                 $this->removedFields[] = $name;
             }

+ 5 - 13
src/Symfony/Component/Form/Field.php

@@ -55,7 +55,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 class Field implements FieldInterface
 {
     private $errors = array();
-    private $key = '';
+    private $name = '';
     private $parent;
     private $submitted = false;
     private $required;
@@ -72,9 +72,9 @@ class Field implements FieldInterface
     private $disabled = false;
     private $dispatcher;
 
-    public function __construct($key, EventDispatcherInterface $dispatcher)
+    public function __construct($name, EventDispatcherInterface $dispatcher)
     {
-        $this->key = (string)$key;
+        $this->name = (string)$name;
         $this->dispatcher = $dispatcher;
     }
 
@@ -153,17 +153,9 @@ class Field implements FieldInterface
     /**
      * {@inheritDoc}
      */
-    public function setKey($key)
+    public function getName()
     {
-        $this->key = (string)$key;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getKey()
-    {
-        return $this->key;
+        return $this->name;
     }
 
     /**

+ 4 - 15
src/Symfony/Component/Form/FieldInterface.php

@@ -47,28 +47,17 @@ interface FieldInterface
     function getParent();
 
     /**
-     * Sets the key by which the field is identified in field groups.
+     * Returns the name by which the field is identified in forms.
      *
-     * Once this field is nested in a field group, i.e. after setParent() was
-     * called for the first time, this method should throw an exception.
-     *
-     * @param  string $key             The key of the field
-     * @throws BadMethodCallException  When the field already has a parent
-     */
-    function setKey($key);
-
-    /**
-     * Returns the key by which the field is identified in field groups.
-     *
-     * @return string  The key of the field.
+     * @return string  The name of the field.
      */
-    function getKey();
+    function getName();
 
     /**
      * Sets the property path
      *
      * The property path determines the property or a sequence of properties
-     * that a field updates in the data of the field group.
+     * that a field updates in the data of the form.
      *
      * @param string $propertyPath
      */

+ 44 - 60
src/Symfony/Component/Form/Form.php

@@ -81,13 +81,13 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
 
     private $csrfProvider;
 
-    public function __construct($key, EventDispatcherInterface $dispatcher,
+    public function __construct($name, EventDispatcherInterface $dispatcher,
             FormFactoryInterface $factory, CsrfProviderInterface $csrfProvider,
             ValidatorInterface $validator)
     {
         $dispatcher->addEventSubscriber($this);
 
-        parent::__construct($key, $dispatcher);
+        parent::__construct($name, $dispatcher);
 
         $this->factory = $factory;
         $this->csrfProvider = $csrfProvider;
@@ -142,14 +142,14 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
                 throw new UnexpectedTypeException($field, 'FieldInterface or string');
             }
 
-            // TODO turn order of $identifier and $key around
+            // TODO turn order of $identifier and $name around
 
             if (func_num_args() > 2 || (func_num_args() > 1 && !is_array(func_get_arg(1)))) {
                 $identifier = func_get_arg(0);
-                $key = func_get_arg(1);
+                $name = func_get_arg(1);
                 $options = func_num_args() > 2 ? func_get_arg(2) : array();
 
-                $field = $this->factory->getInstance($identifier, $key, $options);
+                $field = $this->factory->getInstance($identifier, $name, $options);
             } else {
                 $class = $this->getDataClass();
 
@@ -164,11 +164,11 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
             }
         }
 
-        if ('' === $field->getKey() || null === $field->getKey()) {
+        if ('' === $field->getName() || null === $field->getName()) {
             throw new FieldDefinitionException('You cannot add anonymous fields');
         }
 
-        $this->fields[$field->getKey()] = $field;
+        $this->fields[$field->getName()] = $field;
 
         $field->setParent($this);
 
@@ -184,41 +184,41 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
     }
 
     /**
-     * Removes the field with the given key.
+     * Removes the field with the given name.
      *
-     * @param string $key
+     * @param string $name
      */
-    public function remove($key)
+    public function remove($name)
     {
-        $this->fields[$key]->setParent(null);
+        $this->fields[$name]->setParent(null);
 
-        unset($this->fields[$key]);
+        unset($this->fields[$name]);
     }
 
     /**
-     * Returns whether a field with the given key exists.
+     * Returns whether a field with the given name exists.
      *
-     * @param  string $key
+     * @param  string $name
      * @return Boolean
      */
-    public function has($key)
+    public function has($name)
     {
-        return isset($this->fields[$key]);
+        return isset($this->fields[$name]);
     }
 
     /**
-     * Returns the field with the given key.
+     * Returns the field with the given name.
      *
-     * @param  string $key
+     * @param  string $name
      * @return FieldInterface
      */
-    public function get($key)
+    public function get($name)
     {
-        if (isset($this->fields[$key])) {
-            return $this->fields[$key];
+        if (isset($this->fields[$name])) {
+            return $this->fields[$name];
         }
 
-        throw new \InvalidArgumentException(sprintf('Field "%s" does not exist.', $key));
+        throw new \InvalidArgumentException(sprintf('Field "%s" does not exist.', $name));
     }
 
     /**
@@ -287,15 +287,15 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
             throw new UnexpectedTypeException($data, 'array');
         }
 
-        foreach ($this->fields as $key => $field) {
-            if (!isset($data[$key])) {
-                $data[$key] = null;
+        foreach ($this->fields as $name => $field) {
+            if (!isset($data[$name])) {
+                $data[$name] = null;
             }
         }
 
-        foreach ($data as $key => $value) {
-            if ($this->has($key)) {
-                $this->fields[$key]->submit($value);
+        foreach ($data as $name => $value) {
+            if ($this->has($name)) {
+                $this->fields[$name]->submit($value);
             }
         }
 
@@ -324,8 +324,8 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
     {
         $values = array();
 
-        foreach ($this->fields as $key => $field) {
-            $values[$key] = $field->getDisplayedData();
+        foreach ($this->fields as $name => $field) {
+            $values[$name] = $field->getDisplayedData();
         }
 
         return $values;
@@ -343,9 +343,9 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
 
         $this->extraFields = array();
 
-        foreach ((array)$data as $key => $value) {
-            if (!$this->has($key)) {
-                $this->extraFields[] = $key;
+        foreach ((array)$data as $name => $value) {
+            if (!$this->has($name)) {
+                $this->extraFields[] = $name;
             }
         }
     }
@@ -512,25 +512,25 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
     /**
      * Returns true if the field exists (implements the \ArrayAccess interface).
      *
-     * @param string $key The key of the field
+     * @param string $name The name of the field
      *
      * @return Boolean true if the widget exists, false otherwise
      */
-    public function offsetExists($key)
+    public function offsetExists($name)
     {
-        return $this->has($key);
+        return $this->has($name);
     }
 
     /**
      * Returns the form field associated with the name (implements the \ArrayAccess interface).
      *
-     * @param string $key The offset of the value to get
+     * @param string $name The offset of the value to get
      *
      * @return Field A form field instance
      */
-    public function offsetGet($key)
+    public function offsetGet($name)
     {
-        return $this->get($key);
+        return $this->get($name);
     }
 
     /**
@@ -541,7 +541,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
      *
      * @throws \LogicException
      */
-    public function offsetSet($key, $field)
+    public function offsetSet($name, $field)
     {
         throw new \LogicException('Use the method add() to add fields');
     }
@@ -549,13 +549,13 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
     /**
      * Throws an exception saying that values cannot be unset (implements the \ArrayAccess interface).
      *
-     * @param string $key
+     * @param string $name
      *
      * @throws \LogicException
      */
-    public function offsetUnset($key)
+    public function offsetUnset($name)
     {
-        return $this->remove($key);
+        return $this->remove($name);
     }
 
     /**
@@ -699,7 +699,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
      */
     public function bind(Request $request, $data = null)
     {
-        if (!$this->getKey()) {
+        if (!$this->getName()) {
             throw new FormException('You cannot bind anonymous forms. Please give this form a name');
         }
 
@@ -720,22 +720,6 @@ class Form extends Field implements \IteratorAggregate, FormInterface, EventSubs
         }
     }
 
-    /**
-     * @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
      *

+ 4 - 4
src/Symfony/Component/Form/FormFactory.php

@@ -32,7 +32,7 @@ class FormFactory implements FormFactoryInterface
         $this->guessers[] = $guesser;
     }
 
-    public function getInstance($identifier, $key = null, array $options = array())
+    public function getInstance($identifier, $name = null, array $options = array())
     {
         // TODO $identifier can be FQN of a config class
 
@@ -40,15 +40,15 @@ class FormFactory implements FormFactoryInterface
         $hierarchy = array();
 
         // TESTME
-        if (null === $key) {
-            $key = $identifier;
+        if (null === $name) {
+            $name = $identifier;
         }
 
         while (null !== $identifier) {
             // TODO check if identifier exists
             $config = $this->configLoader->getConfig($identifier);
             array_unshift($hierarchy, $config);
-            $instance = $instance ?: $config->createInstance($key);
+            $instance = $instance ?: $config->createInstance($name);
             $options = array_merge($config->getDefaultOptions($options), $options);
             $identifier = $config->getParent($options);
         }

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

@@ -13,7 +13,7 @@ namespace Symfony\Component\Form;
 
 interface FormFactoryInterface
 {
-    function getInstance($identifier, $key = null, array $options = array());
+    function getInstance($identifier, $name = null, array $options = array());
 
     function getInstanceForProperty($class, $property, array $options = array());
 }

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

@@ -34,7 +34,7 @@ class FieldPlugin implements PluginInterface
      */
     public function setUp(RendererInterface $renderer)
     {
-        $fieldKey = $this->field->getKey();
+        $fieldKey = $this->field->getName();
 
         if ($this->field->hasParent()) {
             $parentRenderer = $this->field->getParent()->getRenderer();
@@ -57,6 +57,6 @@ class FieldPlugin implements PluginInterface
         $renderer->setVar('class', null);
         $renderer->setVar('max_length', null);
         $renderer->setVar('size', null);
-        $renderer->setVar('label', ucfirst(strtolower(str_replace('_', ' ', $this->field->getKey()))));
+        $renderer->setVar('label', ucfirst(strtolower(str_replace('_', ' ', $this->field->getName()))));
     }
 }

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

@@ -27,8 +27,8 @@ class FormPlugin implements PluginInterface
     {
         $fields = array();
 
-        foreach ($this->form as $key => $field) {
-            $fields[$key] = $field->getRenderer();
+        foreach ($this->form as $name => $field) {
+            $fields[$name] = $field->getRenderer();
         }
 
         $renderer->setVar('fields', $fields);

+ 3 - 3
tests/Symfony/Tests/Component/Form/CollectionFieldTest.php

@@ -60,13 +60,13 @@ class CollectionFieldTest extends TestCase
 
         $this->assertTrue($field[0] instanceof Field);
         $this->assertTrue($field[1] instanceof Field);
-        $this->assertTrue($field['$$key$$'] instanceof Field);
+        $this->assertTrue($field['$$name$$'] instanceof Field);
         $this->assertEquals(3, count($field));
 
         $field->setData(array('foo@baz.com'));
         $this->assertTrue($field[0] instanceof Field);
         $this->assertFalse(isset($field[1]));
-        $this->assertTrue($field['$$key$$'] instanceof Field);
+        $this->assertTrue($field['$$name$$'] instanceof Field);
         $this->assertEquals(2, count($field));
     }
 
@@ -88,7 +88,7 @@ class CollectionFieldTest extends TestCase
         $field->setData(array('foo@bar.com'));
 
         $this->assertTrue($field['0'] instanceof Field);
-        $this->assertTrue($field['$$key$$'] instanceof Field);
+        $this->assertTrue($field['$$name$$'] instanceof Field);
         $this->assertEquals(2, count($field));
     }
 

+ 1 - 1
tests/Symfony/Tests/Component/Form/FormTest.php

@@ -1323,7 +1323,7 @@ class FormTest extends TestCase
         );
 
         $field->expects($this->any())
-                    ->method('getKey')
+                    ->method('getName')
                     ->will($this->returnValue($key));
 
         return $field;