@@ -522,83 +522,82 @@ public function submit($submittedData, $clearMissing = true)
522
522
523
523
$ dispatcher = $ this ->config ->getEventDispatcher ();
524
524
525
- // Hook to change content of the data submitted by the browser
526
- if ($ dispatcher ->hasListeners (FormEvents::PRE_SUBMIT )) {
527
- $ event = new FormEvent ($ this , $ submittedData );
528
- $ dispatcher ->dispatch (FormEvents::PRE_SUBMIT , $ event );
529
- $ submittedData = $ event ->getData ();
530
- }
525
+ $ modelData = null ;
526
+ $ normData = null ;
527
+ $ viewData = null ;
531
528
532
- // Check whether the form is compound.
533
- // This check is preferable over checking the number of children,
534
- // since forms without children may also be compound.
535
- // (think of empty collection forms)
536
- if ($ this ->config ->getCompound ()) {
537
- if (!is_array ($ submittedData )) {
538
- $ submittedData = array ();
529
+ try {
530
+ // Hook to change content of the data submitted by the browser
531
+ if ($ dispatcher ->hasListeners (FormEvents::PRE_SUBMIT )) {
532
+ $ event = new FormEvent ($ this , $ submittedData );
533
+ $ dispatcher ->dispatch (FormEvents::PRE_SUBMIT , $ event );
534
+ $ submittedData = $ event ->getData ();
539
535
}
540
536
541
- foreach ($ this ->children as $ name => $ child ) {
542
- if (array_key_exists ($ name , $ submittedData ) || $ clearMissing ) {
543
- $ child ->submit (isset ($ submittedData [$ name ]) ? $ submittedData [$ name ] : null , $ clearMissing );
544
- unset($ submittedData [$ name ]);
537
+ // Check whether the form is compound.
538
+ // This check is preferable over checking the number of children,
539
+ // since forms without children may also be compound.
540
+ // (think of empty collection forms)
541
+ if ($ this ->config ->getCompound ()) {
542
+ if (null === $ submittedData ) {
543
+ $ submittedData = array ();
545
544
}
546
- }
547
-
548
- $ this ->extraData = $ submittedData ;
549
- }
550
545
551
- // Forms that inherit their parents' data also are not processed,
552
- // because then it would be too difficult to merge the changes in
553
- // the child and the parent form. Instead, the parent form also takes
554
- // changes in the grandchildren (i.e. children of the form that inherits
555
- // its parent's data) into account.
556
- // (see InheritDataAwareIterator below)
557
- if ($ this ->config ->getInheritData ()) {
558
- $ this ->submitted = true ;
546
+ if (!is_array ($ submittedData )) {
547
+ throw new TransformationFailedException ('Compound forms expect an array or NULL on submission. ' );
548
+ }
559
549
560
- // When POST_SUBMIT is reached, the data is not yet updated, so pass
561
- // NULL to prevent hard-to-debug bugs.
562
- $ dataForPostSubmit = null ;
563
- } else {
564
- // If the form is compound, the default data in view format
565
- // is reused. The data of the children is merged into this
566
- // default data using the data mapper.
567
- // If the form is not compound, the submitted data is also the data in view format.
568
- $ viewData = $ this ->config ->getCompound () ? $ this ->viewData : $ submittedData ;
569
-
570
- if (FormUtil::isEmpty ($ viewData )) {
571
- $ emptyData = $ this ->config ->getEmptyData ();
572
-
573
- if ($ emptyData instanceof \Closure) {
574
- /* @var \Closure $emptyData */
575
- $ emptyData = $ emptyData ($ this , $ viewData );
550
+ foreach ($ this ->children as $ name => $ child ) {
551
+ if (isset ($ submittedData [$ name ]) || $ clearMissing ) {
552
+ $ child ->submit (isset ($ submittedData [$ name ]) ? $ submittedData [$ name ] : null , $ clearMissing );
553
+ unset($ submittedData [$ name ]);
554
+ }
576
555
}
577
556
578
- $ viewData = $ emptyData ;
557
+ $ this -> extraData = $ submittedData ;
579
558
}
580
559
581
- // Merge form data from children into existing view data
582
- // It is not necessary to invoke this method if the form has no children,
583
- // even if it is compound.
584
- if (count ($ this ->children ) > 0 ) {
585
- // Use InheritDataAwareIterator to process children of
586
- // descendants that inherit this form's data.
587
- // These descendants will not be submitted normally (see the check
588
- // for $this->config->getInheritData() above)
589
- $ iterator = new InheritDataAwareIterator ($ this ->children );
590
- $ iterator = new \RecursiveIteratorIterator ($ iterator );
591
- $ this ->config ->getDataMapper ()->mapFormsToData ($ iterator , $ viewData );
592
- }
560
+ // Forms that inherit their parents' data also are not processed,
561
+ // because then it would be too difficult to merge the changes in
562
+ // the child and the parent form. Instead, the parent form also takes
563
+ // changes in the grandchildren (i.e. children of the form that inherits
564
+ // its parent's data) into account.
565
+ // (see InheritDataAwareIterator below)
566
+ if (!$ this ->config ->getInheritData ()) {
567
+ // If the form is compound, the default data in view format
568
+ // is reused. The data of the children is merged into this
569
+ // default data using the data mapper.
570
+ // If the form is not compound, the submitted data is also the data in view format.
571
+ $ viewData = $ this ->config ->getCompound () ? $ this ->viewData : $ submittedData ;
572
+
573
+ if (FormUtil::isEmpty ($ viewData )) {
574
+ $ emptyData = $ this ->config ->getEmptyData ();
575
+
576
+ if ($ emptyData instanceof \Closure) {
577
+ /* @var \Closure $emptyData */
578
+ $ emptyData = $ emptyData ($ this , $ viewData );
579
+ }
580
+
581
+ $ viewData = $ emptyData ;
582
+ }
593
583
594
- $ modelData = null ;
595
- $ normData = null ;
584
+ // Merge form data from children into existing view data
585
+ // It is not necessary to invoke this method if the form has no children,
586
+ // even if it is compound.
587
+ if (count ($ this ->children ) > 0 ) {
588
+ // Use InheritDataAwareIterator to process children of
589
+ // descendants that inherit this form's data.
590
+ // These descendants will not be submitted normally (see the check
591
+ // for $this->config->getInheritData() above)
592
+ $ childrenIterator = new InheritDataAwareIterator ($ this ->children );
593
+ $ childrenIterator = new \RecursiveIteratorIterator ($ childrenIterator );
594
+ $ this ->config ->getDataMapper ()->mapFormsToData ($ childrenIterator , $ viewData );
595
+ }
596
596
597
- try {
598
597
// Normalize data to unified representation
599
598
$ normData = $ this ->viewToNorm ($ viewData );
600
599
601
- // Hook to change content of the data into the normalized
600
+ // Hook to change content of the data in the normalized
602
601
// representation
603
602
if ($ dispatcher ->hasListeners (FormEvents::SUBMIT )) {
604
603
$ event = new FormEvent ($ this , $ normData );
@@ -609,20 +608,26 @@ public function submit($submittedData, $clearMissing = true)
609
608
// Synchronize representations - must not change the content!
610
609
$ modelData = $ this ->normToModel ($ normData );
611
610
$ viewData = $ this ->normToView ($ normData );
612
- } catch (TransformationFailedException $ e ) {
613
- $ this ->synchronized = false ;
614
611
}
615
-
616
- $ this ->submitted = true ;
617
- $ this ->modelData = $ modelData ;
618
- $ this ->normData = $ normData ;
619
- $ this ->viewData = $ viewData ;
620
-
621
- $ dataForPostSubmit = $ viewData ;
612
+ } catch (TransformationFailedException $ e ) {
613
+ $ this ->synchronized = false ;
614
+
615
+ // If $viewData was not yet set, set it to $submittedData so that
616
+ // the erroneous data is accessible on the form.
617
+ // Forms that inherit data never set any data, because the getters
618
+ // forward to the parent form's getters anyway.
619
+ if (null === $ viewData && !$ this ->config ->getInheritData ()) {
620
+ $ viewData = $ submittedData ;
621
+ }
622
622
}
623
623
624
+ $ this ->submitted = true ;
625
+ $ this ->modelData = $ modelData ;
626
+ $ this ->normData = $ normData ;
627
+ $ this ->viewData = $ viewData ;
628
+
624
629
if ($ dispatcher ->hasListeners (FormEvents::POST_SUBMIT )) {
625
- $ event = new FormEvent ($ this , $ dataForPostSubmit );
630
+ $ event = new FormEvent ($ this , $ viewData );
626
631
$ dispatcher ->dispatch (FormEvents::POST_SUBMIT , $ event );
627
632
}
628
633
0 commit comments