@@ -333,13 +333,18 @@ public function setData($modelData)
333
333
}
334
334
335
335
$ this ->lockSetData = true ;
336
- $ dispatcher = $ this ->config ->getEventDispatcher ();
336
+
337
+ // Collecting $this + all children with "inherit_data" option
338
+ $ thisModelDataAwareForms = $ this ->getSameModelAwareForms ($ this );
337
339
338
340
// Hook to change content of the model data before transformation and mapping children
339
- if ($ dispatcher ->hasListeners (FormEvents::PRE_SET_DATA )) {
340
- $ event = new PreSetDataEvent ($ this , $ modelData );
341
- $ dispatcher ->dispatch ($ event , FormEvents::PRE_SET_DATA );
342
- $ modelData = $ event ->getData ();
341
+ foreach ($ thisModelDataAwareForms as $ form ) {
342
+ $ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
343
+ if ($ dispatcher ->hasListeners (FormEvents::PRE_SET_DATA )) {
344
+ $ event = new PreSetDataEvent ($ form , $ modelData );
345
+ $ dispatcher ->dispatch ($ event , FormEvents::PRE_SET_DATA );
346
+ $ modelData = $ event ->getData ();
347
+ }
343
348
}
344
349
345
350
// Treat data as strings unless a transformer exists
@@ -359,7 +364,7 @@ public function setData($modelData)
359
364
if (null !== $ dataClass && !$ viewData instanceof $ dataClass ) {
360
365
$ actualType = get_debug_type ($ viewData );
361
366
362
- throw new LogicException ('The form \'s view data is expected to be a " ' .$ dataClass .'", but it is a " ' .$ actualType .'". You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms " ' .$ actualType .'" to an instance of " ' .$ dataClass .'". ' );
367
+ throw new LogicException ('The form \'s view data is expected to be a ". ' .$ dataClass .'", but it is a " ' .$ actualType .'". You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms " ' .$ actualType .'" to an instance of " ' .$ dataClass .'". ' );
363
368
}
364
369
}
365
370
@@ -374,10 +379,12 @@ public function setData($modelData)
374
379
// Update child forms from the data (unless their config data is locked)
375
380
$ this ->config ->getDataMapper ()->mapDataToForms ($ viewData , new \RecursiveIteratorIterator (new InheritDataAwareIterator ($ this ->children )));
376
381
}
377
-
378
- if ($ dispatcher ->hasListeners (FormEvents::POST_SET_DATA )) {
379
- $ event = new PostSetDataEvent ($ this , $ modelData );
380
- $ dispatcher ->dispatch ($ event , FormEvents::POST_SET_DATA );
382
+ foreach ($ thisModelDataAwareForms as $ form ) {
383
+ $ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
384
+ if ($ dispatcher ->hasListeners (FormEvents::POST_SET_DATA )) {
385
+ $ event = new PostSetDataEvent ($ form , $ modelData );
386
+ $ dispatcher ->dispatch ($ event , FormEvents::POST_SET_DATA );
387
+ }
381
388
}
382
389
383
390
return $ this ;
@@ -538,7 +545,8 @@ public function submit($submittedData, bool $clearMissing = true)
538
545
$ this ->transformationFailure = new TransformationFailedException ('Submitted data was expected to be text or number, array given. ' );
539
546
}
540
547
541
- $ dispatcher = $ this ->config ->getEventDispatcher ();
548
+ // Collecting $this + all children with "inherit_data" option only if $this is not using "inherit_data" option
549
+ $ thisModelDataAwareForms = !$ this ->getConfig ()->getInheritData () ? $ this ->getSameModelAwareForms ($ this ) : [];
542
550
543
551
$ modelData = null ;
544
552
$ normData = null ;
@@ -550,10 +558,13 @@ public function submit($submittedData, bool $clearMissing = true)
550
558
}
551
559
552
560
// Hook to change content of the data submitted by the browser
553
- if ($ dispatcher ->hasListeners (FormEvents::PRE_SUBMIT )) {
554
- $ event = new PreSubmitEvent ($ this , $ submittedData );
555
- $ dispatcher ->dispatch ($ event , FormEvents::PRE_SUBMIT );
556
- $ submittedData = $ event ->getData ();
561
+ foreach ($ thisModelDataAwareForms as $ form ) {
562
+ $ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
563
+ if ($ dispatcher ->hasListeners (FormEvents::PRE_SUBMIT )) {
564
+ $ event = new PreSubmitEvent ($ form , $ submittedData );
565
+ $ dispatcher ->dispatch ($ event , FormEvents::PRE_SUBMIT );
566
+ $ submittedData = $ event ->getData ();
567
+ }
557
568
}
558
569
559
570
// Check whether the form is compound.
@@ -636,10 +647,13 @@ public function submit($submittedData, bool $clearMissing = true)
636
647
637
648
// Hook to change content of the data in the normalized
638
649
// representation
639
- if ($ dispatcher ->hasListeners (FormEvents::SUBMIT )) {
640
- $ event = new SubmitEvent ($ this , $ normData );
641
- $ dispatcher ->dispatch ($ event , FormEvents::SUBMIT );
642
- $ normData = $ event ->getData ();
650
+ foreach ($ thisModelDataAwareForms as $ form ) {
651
+ $ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
652
+ if ($ dispatcher ->hasListeners (FormEvents::SUBMIT )) {
653
+ $ event = new SubmitEvent ($ form , $ normData );
654
+ $ dispatcher ->dispatch ($ event , FormEvents::SUBMIT );
655
+ $ normData = $ event ->getData ();
656
+ }
643
657
}
644
658
645
659
// Synchronize representations - must not change the content!
@@ -663,14 +677,33 @@ public function submit($submittedData, bool $clearMissing = true)
663
677
$ this ->normData = $ normData ;
664
678
$ this ->viewData = $ viewData ;
665
679
666
- if ($ dispatcher ->hasListeners (FormEvents::POST_SUBMIT )) {
667
- $ event = new PostSubmitEvent ($ this , $ viewData );
668
- $ dispatcher ->dispatch ($ event , FormEvents::POST_SUBMIT );
680
+ foreach ($ thisModelDataAwareForms as $ form ) {
681
+ $ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
682
+ if ($ dispatcher ->hasListeners (FormEvents::POST_SUBMIT )) {
683
+ $ event = new PostSubmitEvent ($ form , $ viewData );
684
+ $ dispatcher ->dispatch ($ event , FormEvents::POST_SUBMIT );
685
+ }
669
686
}
670
687
671
688
return $ this ;
672
689
}
673
690
691
+ /**
692
+ * @return array<FormInterface>
693
+ */
694
+ private function getSameModelAwareForms (self $ form ): array
695
+ {
696
+ return array_reduce (iterator_to_array ($ form ->children ),
697
+ static function (array $ children , FormInterface $ child ): array {
698
+ if ($ child ->getConfig ()->getInheritData ()) {
699
+ $ children [] = $ child ;
700
+ }
701
+
702
+ return $ children ;
703
+ }, [$ form ]
704
+ );
705
+ }
706
+
674
707
/**
675
708
* {@inheritdoc}
676
709
*/
0 commit comments