This repository was archived by the owner on Jan 30, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +46
-4
lines changed Expand file tree Collapse file tree 2 files changed +46
-4
lines changed Original file line number Diff line number Diff line change 11
11
12
12
use Traversable ;
13
13
use Zend \Code \Reflection \ClassReflection ;
14
+ use Zend \Form \Element \Collection ;
14
15
use Zend \Hydrator ;
15
16
use Zend \Hydrator \HydratorAwareInterface ;
16
17
use Zend \Hydrator \HydratorInterface ;
@@ -564,12 +565,18 @@ public function bindValues(array $values = [])
564
565
$ hydrator = $ this ->getHydrator ();
565
566
$ hydratableData = [];
566
567
567
- foreach ($ values as $ name => $ value ) {
568
- if (!$ this ->has ($ name )) {
569
- continue ;
568
+ foreach ($ this ->iterator as $ element ) {
569
+ $ name = $ element ->getName ();
570
+
571
+ if (!array_key_exists ($ name , $ values )) {
572
+ if (!($ element instanceof Collection)) {
573
+ continue ;
574
+ }
575
+
576
+ $ values [$ name ] = [];
570
577
}
571
578
572
- $ element = $ this -> iterator -> get ( $ name) ;
579
+ $ value = $ values [ $ name] ;
573
580
574
581
if ($ element instanceof FieldsetInterface && $ element ->allowValueBinding ()) {
575
582
$ value = $ element ->bindValues ($ value );
Original file line number Diff line number Diff line change @@ -2135,4 +2135,39 @@ public function testSetValidationGroupOnFormWithNestedCollectionsPopulatesOnlyFi
2135
2135
2136
2136
$ this ->assertEquals ($ data , $ this ->form ->getData ());
2137
2137
}
2138
+
2139
+ public function testShouldHydrateEmptyCollection ()
2140
+ {
2141
+ $ fieldset = new Fieldset ('example ' );
2142
+ $ fieldset ->add ([
2143
+ 'type ' => 'Zend\Form\Element\Collection ' ,
2144
+ 'name ' => 'foo ' ,
2145
+ 'options ' => [
2146
+ 'label ' => 'InputFilterProviderFieldset ' ,
2147
+ 'count ' => 1 ,
2148
+ 'target_element ' => [
2149
+ 'type ' => 'text '
2150
+ ]
2151
+ ],
2152
+ ]);
2153
+
2154
+ $ this ->form ->add ($ fieldset );
2155
+ $ this ->form ->setBaseFieldset ($ fieldset );
2156
+ $ this ->form ->setHydrator (new \Zend \Hydrator \ObjectProperty ());
2157
+
2158
+ $ object = new Entity \SimplePublicProperty ();
2159
+ $ object ->foo = ['item 1 ' , 'item 2 ' ];
2160
+
2161
+ $ this ->form ->bind ($ object );
2162
+
2163
+ $ this ->form ->setData ([
2164
+ 'submit ' => 'Confirm ' ,
2165
+ 'example ' => [
2166
+ //'foo' => [] // $_POST does't have this if collection is empty
2167
+ ]
2168
+ ]);
2169
+
2170
+ $ this ->assertTrue ($ this ->form ->isValid ());
2171
+ $ this ->assertEquals ([], $ this ->form ->getObject ()->foo );
2172
+ }
2138
2173
}
You can’t perform that action at this time.
0 commit comments