24
24
use Symfony \Component \Form \Exception \UnexpectedTypeException ;
25
25
use Symfony \Component \Form \Extension \Core \Type \TextType ;
26
26
use Symfony \Component \Form \Util \FormUtil ;
27
+ use Symfony \Component \Form \Util \InheritDataAwareFilter ;
27
28
use Symfony \Component \Form \Util \InheritDataAwareIterator ;
28
29
use Symfony \Component \Form \Util \OrderedHashMap ;
29
30
use Symfony \Component \PropertyAccess \PropertyPath ;
@@ -282,11 +283,13 @@ public function setData(mixed $modelData): static
282
283
283
284
$ this ->lockSetData = true ;
284
285
285
- // Collecting $this + all children with "inherit_data" option
286
- $ thisModelDataAwareForms = $ this ->getSameModelAwareForms ($ this );
286
+ // Collecting $this + all children with "inherit_data" option TRUE
287
+ $ thisModelDataAwareFormsIterator = new \AppendIterator ();
288
+ $ thisModelDataAwareFormsIterator ->append (new \ArrayIterator ([$ this ]));
289
+ $ thisModelDataAwareFormsIterator ->append (new InheritDataAwareFilter (new InheritDataAwareIterator ($ this ->children )));
287
290
288
291
// Hook to change content of the model data before transformation and mapping children
289
- foreach ($ thisModelDataAwareForms as $ form ) {
292
+ foreach ($ thisModelDataAwareFormsIterator as $ form ) {
290
293
$ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
291
294
if ($ dispatcher ->hasListeners (FormEvents::PRE_SET_DATA )) {
292
295
$ event = new PreSetDataEvent ($ form , $ modelData );
@@ -327,7 +330,8 @@ public function setData(mixed $modelData): static
327
330
// Update child forms from the data (unless their config data is locked)
328
331
$ this ->config ->getDataMapper ()->mapDataToForms ($ viewData , new \RecursiveIteratorIterator (new InheritDataAwareIterator ($ this ->children )));
329
332
}
330
- foreach ($ thisModelDataAwareForms as $ form ) {
333
+
334
+ foreach ($ thisModelDataAwareFormsIterator as $ form ) {
331
335
$ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
332
336
if ($ dispatcher ->hasListeners (FormEvents::POST_SET_DATA )) {
333
337
$ event = new PostSetDataEvent ($ form , $ modelData );
@@ -472,8 +476,13 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static
472
476
$ this ->transformationFailure = new TransformationFailedException ('Submitted data was expected to be text or number, array given. ' );
473
477
}
474
478
475
- // Collecting $this + all children with "inherit_data" option only if $this is not using "inherit_data" option
476
- $ thisModelDataAwareForms = !$ this ->getConfig ()->getInheritData () ? $ this ->getSameModelAwareForms ($ this ) : [];
479
+ $ thisModelDataAwareFormsIterator = new \AppendIterator ();
480
+
481
+ // Collecting $this + all children with "inherit_data" option TRUE only if $this has "inherit_data" option FALSE to avoid to dispatch events two times.
482
+ if (!$ this ->getConfig ()->getInheritData ()) {
483
+ $ thisModelDataAwareFormsIterator ->append (new \ArrayIterator (['' => $ this ]));
484
+ $ thisModelDataAwareFormsIterator ->append (new InheritDataAwareFilter (new InheritDataAwareIterator ($ this ->children )));
485
+ }
477
486
478
487
$ modelData = null ;
479
488
$ normData = null ;
@@ -485,10 +494,16 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static
485
494
}
486
495
487
496
// Hook to change content of the data submitted by the browser
488
- foreach ($ thisModelDataAwareForms as $ form ) {
497
+ foreach ($ thisModelDataAwareFormsIterator as $ name => $ form ) {
489
498
$ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
490
499
if ($ dispatcher ->hasListeners (FormEvents::PRE_SUBMIT )) {
491
- $ event = new PreSubmitEvent ($ form , $ submittedData );
500
+ $ eventSubmittedData = null ;
501
+ if (empty ($ name )) {
502
+ $ eventSubmittedData = $ submittedData ;
503
+ } elseif (\array_key_exists ($ name , $ submittedData )) {
504
+ $ eventSubmittedData = $ submittedData [$ name ];
505
+ }
506
+ $ event = new PreSubmitEvent ($ form , $ eventSubmittedData );
492
507
$ dispatcher ->dispatch ($ event , FormEvents::PRE_SUBMIT );
493
508
$ submittedData = $ event ->getData ();
494
509
}
@@ -574,7 +589,7 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static
574
589
575
590
// Hook to change content of the data in the normalized
576
591
// representation
577
- foreach ($ thisModelDataAwareForms as $ form ) {
592
+ foreach ($ thisModelDataAwareFormsIterator as $ form ) {
578
593
$ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
579
594
if ($ dispatcher ->hasListeners (FormEvents::SUBMIT )) {
580
595
$ event = new SubmitEvent ($ form , $ normData );
@@ -604,7 +619,7 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static
604
619
$ this ->normData = $ normData ;
605
620
$ this ->viewData = $ viewData ;
606
621
607
- foreach ($ thisModelDataAwareForms as $ form ) {
622
+ foreach ($ thisModelDataAwareFormsIterator as $ form ) {
608
623
$ dispatcher = $ form ->getConfig ()->getEventDispatcher ();
609
624
if ($ dispatcher ->hasListeners (FormEvents::POST_SUBMIT )) {
610
625
$ event = new PostSubmitEvent ($ form , $ viewData );
@@ -615,22 +630,6 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static
615
630
return $ this ;
616
631
}
617
632
618
- /**
619
- * @return array<FormInterface>
620
- */
621
- private function getSameModelAwareForms (self $ form ): array
622
- {
623
- return array_reduce (iterator_to_array ($ form ->children ),
624
- static function (array $ children , FormInterface $ child ): array {
625
- if ($ child ->getConfig ()->getInheritData ()) {
626
- $ children [] = $ child ;
627
- }
628
-
629
- return $ children ;
630
- }, [$ form ]
631
- );
632
- }
633
-
634
633
public function addError (FormError $ error ): static
635
634
{
636
635
if (null === $ error ->getOrigin ()) {
0 commit comments