8000 [Form] Fixed treatment of countables and traversables in Form::isEmpty() · symfony/symfony@03b880f · GitHub
[go: up one dir, main page]

Skip to content

Commit 03b880f

Browse files
committed
[Form] Fixed treatment of countables and traversables in Form::isEmpty()
1 parent 6e499a3 commit 03b880f

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,11 @@ public function isEmpty()
684684
}
685685
}
686686

687-
return FormUtil::isEmpty($this->modelData) || array() === $this->modelData;
687+
return FormUtil::isEmpty($this->modelData) ||
688+
// arrays, countables
689+
0 === count($this->modelData) ||
690+
// traversables that are not countable
691+
($this->modelData instanceof \Traversable && 0 === iterator_count($this->modelData));
688692
}
689693

690694
/**

src/Symfony/Component/Form/FormInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function getErrors();
9898
/**
9999
* Updates the form with default data.
100100
*
101-
* @param array $modelData The data formatted as expected for the underlying object
101+
* @param mixed $modelData The data formatted as expected for the underlying object
102102
*
103103
* @return FormInterface The form instance
104104
*

src/Symfony/Component/Form/Tests/SimpleFormTest.php

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,42 @@
1616
use Symfony\Component\Form\FormEvents;
1717
use Symfony\Component\Form\Util\PropertyPath;
1818
use Symfony\Component\Form\FormConfigBuilder;
19-
use Symfony\Component\Form\FormView;
2019
use Symfony\Component\Form\FormError;
2120
use Symfony\Component\Form\Exception\TransformationFailedException;
2221
use Symfony\Component\EventDispatcher\EventDispatcher;
2322
use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
2423
use Symfony\Component\Form\Tests\Fixtures\FixedFilterListener;
2524

25+
class SimpleFormTest_Countable implements \Countable
26+
{
27+
private $count;
28+
29+
public function __construct($count)
30+
{
31+
$this->count = $count;
32+
}
33+
34+
public function count()
35+
{
36+
return $this->count;
37+
}
38+
}
39+
40+
class SimpleFormTest_Traversable implements \IteratorAggregate
41+
{
42+
private $iterator;
43+
44+
public function __construct($count)
45+
{
46+
$this->iterator = new \ArrayIterator($count > 0 ? array_fill(0, $count, 'Foo') : array());
47+
}
48+
49+
public function getIterator()
50+
{
51+
return $this->iterator;
52+
}
53+
}
54+
2655
class SimpleFormTest extends AbstractFormTest
2756
{
2857
public function testDataIsInitializedToConfiguredValue()
@@ -68,7 +97,7 @@ public function testDataIsInitializedFromBind()
6897
}
6998

7099
/**
71-
* @expectedException Symfony\Component\Form\Exception\AlreadyBoundException
100+
* @expectedException \Symfony\Component\Form\Exception\AlreadyBoundException
72101
*/
73102
public function testBindThrowsExceptionIfAlreadyBound()
74103
{
@@ -173,6 +202,42 @@ public function testEmptyIfEmptyArray()
173202
$this->assertTrue($this->form->isEmpty());
174203
}
175204

205+
public function testEmptyIfEmptyCountable()
206+
{
207+
$this->form = new Form(new FormConfigBuilder('name', __NAMESPACE__ . '\SimpleFormTest_Countable', $this->dispatcher));
208+
209+
$this->form->setData(new SimpleFormTest_Countable(0));
210+
211+
$this->assertTrue($this->form->isEmpty());
212+
}
213+
214+
public function testNotEmptyIfFilledCountable()
215+
{
216+
$this->form = new Form(new FormConfigBuilder('name', __NAMESPACE__ . '\SimpleFormTest_Countable', $this->dispatcher));
217+
218+
$this->form->setData(new SimpleFormTest_Countable(1));
219+
220+
$this->assertFalse($this->form->isEmpty());
221+
}
222+
223+
public function testEmptyIfEmptyTraversable()
224+
{
225+
$this->form = new Form(new FormConfigBuilder('name', __NAMESPACE__ . '\SimpleFormTest_Traversable', $this->dispatcher));
226+
227+
$this->form->setData(new SimpleFormTest_Traversable(0));
228+
229+
$this->assertTrue($this->form->isEmpty());
230+
}
231+
232+
public function testNotEmptyIfFilledTraversable()
233+
{
234+
$this->form = new Form(new FormConfigBuilder('name', __NAMESPACE__ . '\SimpleFormTest_Traversable', $this->dispatcher));
235+
236+
$this->form->setData(new SimpleFormTest_Traversable(1));
237+
238+
$this->assertFalse($this->form->isEmpty());
239+
}
240+
176241
public function testEmptyIfNull()
177242
{
178243
$this->form->setData(null);
@@ -240,7 +305,7 @@ public function testHasNoErrors()
240305
}
241306

242307
/**
243-
* @expectedException Symfony\Component\Form\Exception\AlreadyBoundException
308+
* @expectedException \Symfony\Component\Form\Exception\AlreadyBoundException
244309
*/
245310
public function testSetParentThrowsExceptionIfAlreadyBound()
246311
{
@@ -262,7 +327,7 @@ public function testNotBound()
262327
}
263328

264329
/**
265-
* @expectedException Symfony\Component\Form\Exception\AlreadyBoundException
330+
* @expectedException \Symfony\Component\Form\Exception\AlreadyBoundException
266331
*/
267332
public function testSetDataThrowsExceptionIfAlreadyBound()
268333
{
@@ -674,7 +739,7 @@ public function testSetNullParentWorksWithEmptyName()
674739
}
675740

676741
/**
677-
* @expectedException Symfony\Component\Form\Exception\FormException
742+
* @expectedException \Symfony\Component\Form\Exception\FormException
678743
* @expectedExceptionMessage A form with an empty name cannot have a parent form.
679744
*/
680745
public function testFormCannotHaveEmptyNameNotInRootLevel()
@@ -720,7 +785,7 @@ public function testGetPropertyPathDefaultsToIndexedNameIfParentDataClassIsNull(
720785
}
721786

722787
/**
723-
* @expectedException Symfony\Component\Form\Exception\FormException
788+
* @expectedException \Symfony\Component\Form\Exception\FormException
724789
*/
725790
public function testViewDataMustNotBeObjectIfDataClassIsNull()
726791
{
@@ -750,7 +815,7 @@ public function testViewDataMayBeArrayAccessIfDataClassIsNull()
750815
}
751816

752817
/**
753-
* @expectedException Symfony\Component\Form\Exception\FormException
818+
* @expectedException \Symfony\Component\Form\Exception\FormException
754819
*/
755820
public function testViewDataMustBeObjectIfDataClassIsSet()
756821
{
@@ -765,7 +830,7 @@ public function testViewDataMustBeObjectIfDataClassIsSet()
765830
}
766831

767832
/**
768-
* @expectedException Symfony\Component\Form\Exception\FormException
833+
* @expectedException \Symfony\Component\Form\Exception\FormException
769834
*/
770835
public function testSetDataCannotInvokeItself()
771836
{

0 commit comments

Comments
 (0)
0