Skip to content

Commit

Permalink
[Form] Made RepeatedField sub-field names configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek authored and Bernhard Schussek committed Jan 19, 2011
1 parent d327a90 commit d928632
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
24 changes: 15 additions & 9 deletions src/Symfony/Component/Form/RepeatedField.php
Expand Up @@ -41,17 +41,20 @@ public function __construct(FieldInterface $innerField, array $options = array()
*/
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();
}

/**
Expand All @@ -61,7 +64,7 @@ protected function configure()
*/
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();
}

/**
Expand All @@ -71,7 +74,10 @@ public function isFirstEqualToSecond()
*/
public function setData($data)
{
parent::setData(array('first' => $data, 'second' => $data));
parent::setData(array(
$this->getOption('first_key') => $data,
$this->getOption('second_key') => $data
));
}

/**
Expand All @@ -82,7 +88,7 @@ public function setData($data)
public function getData()
{
if ($this->isBound() && $this->isFirstEqualToSecond()) {
return $this->get('first')->getData();
return $this->get($this->getOption('first_key'))->getData();
}

return null;
Expand Down
16 changes: 8 additions & 8 deletions tests/Symfony/Tests/Component/Form/RepeatedFieldTest.php
Expand Up @@ -29,31 +29,31 @@ public function testSetData()
{
$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());
}

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());
Expand Down

0 comments on commit d928632

Please sign in to comment.