[Form] Fixed RepeatedField not to trigger NotNull/NotBlank errors if any of the fields was filled in

This commit is contained in:
Bernhard Schussek 2011-02-03 13:28:29 +01:00
parent 39c148197f
commit a725415440
2 changed files with 23 additions and 7 deletions

View File

@ -86,16 +86,23 @@ class RepeatedField extends Form
}
/**
* Return only value of first password field.
* Return the value of a child field
*
* @return string The password.
* If the value of the first field is set, this value is returned.
* Otherwise the value of the second field is returned. This way,
* this field will never trigger a NotNull/NotBlank error if any of the
* child fields was filled in.
*
* @return string The field value
*/
public function getData()
{
if ($this->isSubmitted() && $this->isFirstEqualToSecond()) {
return $this->get($this->getOption('first_key'))->getData();
}
// Return whichever data is set. This should not return NULL if any of
// the fields is set, otherwise this might trigger a NotNull/NotBlank
// error even though some value was set
$data1 = $this->get($this->getOption('first_key'))->getData();
$data2 = $this->get($this->getOption('second_key'))->getData();
return null;
return $data1 ?: $data2;
}
}

View File

@ -43,7 +43,7 @@ class RepeatedFieldTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('bar', $this->field['second']->getDisplayedData());
$this->assertFalse($this->field->isFirstEqualToSecond());
$this->assertEquals($input, $this->field->getDisplayedData());
$this->assertEquals(null, $this->field->getData());
$this->assertEquals('foo', $this->field->getData());
}
public function testSubmitEqual()
@ -58,4 +58,13 @@ class RepeatedFieldTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($input, $this->field->getDisplayedData());
$this->assertEquals('foo', $this->field->getData());
}
public function testGetDataReturnsSecondValueIfFirstIsEmpty()
{
$input = array('first' => '', 'second' => 'bar');
$this->field->submit($input);
$this->assertEquals('bar', $this->field->getData());
}
}