Forráskód Böngészése

[Form] Made RepeatedField sub-field names configurable

Jordi Boggiano 14 éve
szülő
commit
d928632583

+ 15 - 9
src/Symfony/Component/Form/RepeatedField.php

@@ -41,17 +41,20 @@ class RepeatedField extends FieldGroup
      */
     protected function configure()
     {
+        $this->addOption('first_key', 'first');
+        $this->addOption('second_key', 'second');
+
+        parent::configure();
+
         $field = clone $this->prototype;
-        $field->setKey('first');
-        $field->setPropertyPath('first');
+        $field->setKey($this->getOption('first_key'));
+        $field->setPropertyPath($this->getOption('first_key'));
         $this->add($field);
 
         $field = clone $this->prototype;
-        $field->setKey('second');
-        $field->setPropertyPath('second');
+        $field->setKey($this->getOption('second_key'));
+        $field->setPropertyPath($this->getOption('second_key'));
         $this->add($field);
-
-        parent::configure();
     }
 
     /**
@@ -61,7 +64,7 @@ class RepeatedField extends FieldGroup
      */
     public function isFirstEqualToSecond()
     {
-        return $this->get('first')->getData() === $this->get('second')->getData();
+        return $this->get($this->getOption('first_key'))->getData() === $this->get($this->getOption('second_key'))->getData();
     }
 
     /**
@@ -71,7 +74,10 @@ class RepeatedField extends FieldGroup
      */
     public function setData($data)
     {
-        parent::setData(array('first' => $data, 'second' => $data));
+        parent::setData(array(
+            $this->getOption('first_key') => $data,
+            $this->getOption('second_key') => $data
+        ));
     }
 
     /**
@@ -82,7 +88,7 @@ class RepeatedField extends FieldGroup
     public function getData()
     {
         if ($this->isBound() && $this->isFirstEqualToSecond()) {
-            return $this->get('first')->getData();
+            return $this->get($this->getOption('first_key'))->getData();
         }
 
         return null;

+ 8 - 8
tests/Symfony/Tests/Component/Form/RepeatedFieldTest.php

@@ -29,18 +29,18 @@ class RepeatedFieldTest extends \PHPUnit_Framework_TestCase
     {
         $this->field->setData('foobar');
 
-        $this->assertEquals('foobar', $this->field['first']->getData());
-        $this->assertEquals('foobar', $this->field['second']->getData());
+        $this->assertEquals('foobar', $this->field['name']->getData());
+        $this->assertEquals('foobar', $this->field['name_repeat']->getData());
     }
 
     public function testBindUnequal()
     {
-        $input = array('first' => 'foo', 'second' => 'bar');
+        $input = array('name' => 'foo', 'name_repeat' => 'bar');
 
         $this->field->bind($input);
 
-        $this->assertEquals('foo', $this->field['first']->getDisplayedData());
-        $this->assertEquals('bar', $this->field['second']->getDisplayedData());
+        $this->assertEquals('foo', $this->field['name']->getDisplayedData());
+        $this->assertEquals('bar', $this->field['name_repeat']->getDisplayedData());
         $this->assertFalse($this->field->isFirstEqualToSecond());
         $this->assertEquals($input, $this->field->getDisplayedData());
         $this->assertEquals(null, $this->field->getData());
@@ -48,12 +48,12 @@ class RepeatedFieldTest extends \PHPUnit_Framework_TestCase
 
     public function testBindEqual()
     {
-        $input = array('first' => 'foo', 'second' => 'foo');
+        $input = array('name' => 'foo', 'name_repeat' => 'foo');
 
         $this->field->bind($input);
 
-        $this->assertEquals('foo', $this->field['first']->getDisplayedData());
-        $this->assertEquals('foo', $this->field['second']->getDisplayedData());
+        $this->assertEquals('foo', $this->field['name']->getDisplayedData());
+        $this->assertEquals('foo', $this->field['name_repeat']->getDisplayedData());
         $this->assertTrue($this->field->isFirstEqualToSecond());
         $this->assertEquals($input, $this->field->getDisplayedData());
         $this->assertEquals('foo', $this->field->getData());