[Form] Made RepeatedField sub-field names configurable

This commit is contained in:
Jordi Boggiano 2011-01-08 23:08:31 +01:00 committed by Bernhard Schussek
parent d327a90ff2
commit d928632583
2 changed files with 26 additions and 20 deletions

View File

@ -41,17 +41,20 @@ class RepeatedField extends FieldGroup
*/
protected function configure()
{
$field = clone $this->prototype;
$field->setKey('first');
$field->setPropertyPath('first');
$this->add($field);
$field = clone $this->prototype;
$field->setKey('second');
$field->setPropertyPath('second');
$this->add($field);
$this->addOption('first_key', 'first');
$this->addOption('second_key', 'second');
parent::configure();
$field = clone $this->prototype;
$field->setKey($this->getOption('first_key'));
$field->setPropertyPath($this->getOption('first_key'));
$this->add($field);
$field = clone $this->prototype;
$field->setKey($this->getOption('second_key'));
$field->setPropertyPath($this->getOption('second_key'));
$this->add($field);
}
/**
@ -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;

View File

@ -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());