Skip to content

Commit a725415

Browse files
author
Bernhard Schussek
committed
[Form] Fixed RepeatedField not to trigger NotNull/NotBlank errors if any of the fields was filled in
1 parent 39c1481 commit a725415

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/Symfony/Component/Form/RepeatedField.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,23 @@ public function setData($data)
8686
}
8787

8888
/**
89-
* Return only value of first password field.
89+
* Return the value of a child field
9090
*
91-
* @return string The password.
91+
* If the value of the first field is set, this value is returned.
92+
* Otherwise the value of the second field is returned. This way,
93+
* this field will never trigger a NotNull/NotBlank error if any of the
94+
* child fields was filled in.
95+
*
96+
* @return string The field value
9297
*/
9398
public function getData()
9499
{
95-
if ($this->isSubmitted() && $this->isFirstEqualToSecond()) {
96-
return $this->get($this->getOption('first_key'))->getData();
97-
}
100+
// Return whichever data is set. This should not return NULL if any of
101+
// the fields is set, otherwise this might trigger a NotNull/NotBlank
102+
// error even though some value was set
103+
$data1 = $this->get($this->getOption('first_key'))->getData();
104+
$data2 = $this->get($this->getOption('second_key'))->getData();
98105

99-
return null;
106+
return $data1 ?: $data2;
100107
}
101108
}

tests/Symfony/Tests/Component/Form/RepeatedFieldTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testSubmitUnequal()
4343
$this->assertEquals('bar', $this->field['second']->getDisplayedData());
4444
$this->assertFalse($this->field->isFirstEqualToSecond());
4545
$this->assertEquals($input, $this->field->getDisplayedData());
46-
$this->assertEquals(null, $this->field->getData());
46+
$this->assertEquals('foo', $this->field->getData());
4747
}
4848

4949
public function testSubmitEqual()
@@ -58,4 +58,13 @@ public function testSubmitEqual()
5858
$this->assertEquals($input, $this->field->getDisplayedData());
5959
$this->assertEquals('foo', $this->field->getData());
6060
}
61+
62+
public function testGetDataReturnsSecondValueIfFirstIsEmpty()
63+
{
64+
$input = array('first' => '', 'second' => 'bar');
65+
66+
$this->field->submit($input);
67+
68+
$this->assertEquals('bar', $this->field->getData());
69+
}
6170
}

0 commit comments

Comments
 (0)