-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Allow Form events to be used as expected with inherit_data
option
#40515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cristoforocervino
wants to merge
3
commits into
symfony:7.3
Choose a base branch
from
cristoforocervino:pre-set-data-event-to-inherit-data-true-children
base: 7.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\Util\FormUtil; | ||
use Symfony\Component\Form\Util\InheritDataAwareFilter; | ||
use Symfony\Component\Form\Util\InheritDataAwareIterator; | ||
use Symfony\Component\Form\Util\OrderedHashMap; | ||
use Symfony\Component\PropertyAccess\PropertyPath; | ||
|
@@ -281,13 +282,20 @@ public function setData(mixed $modelData): static | |
} | ||
|
||
$this->lockSetData = true; | ||
$dispatcher = $this->config->getEventDispatcher(); | ||
|
||
// Collecting $this + all children with "inherit_data" option TRUE | ||
$thisModelDataAwareFormsIterator = new \AppendIterator(); | ||
$thisModelDataAwareFormsIterator->append(new \ArrayIterator([$this])); | ||
$thisModelDataAwareFormsIterator->append(new InheritDataAwareFilter(new InheritDataAwareIterator($this->children))); | ||
|
||
// Hook to change content of the model data before transformation and mapping children | ||
if ($dispatcher->hasListeners(FormEvents::PRE_SET_DATA)) { | ||
$event = new PreSetDataEvent($this, $modelData); | ||
$dispatcher->dispatch($event, FormEvents::PRE_SET_DATA); | ||
$modelData = $event->getData(); | ||
foreach ($thisModelDataAwareFormsIterator as $form) { | ||
$dispatcher = $form->getConfig()->getEventDispatcher(); | ||
if ($dispatcher->hasListeners(FormEvents::PRE_SET_DATA)) { | ||
$event = new PreSetDataEvent($form, $modelData); | ||
$dispatcher->dispatch($event, FormEvents::PRE_SET_DATA); | ||
$modelData = $event->getData(); | ||
} | ||
} | ||
|
||
// Treat data as strings unless a transformer exists | ||
|
@@ -323,9 +331,12 @@ public function setData(mixed $modelData): static | |
$this->config->getDataMapper()->mapDataToForms($viewData, new \RecursiveIteratorIterator(new InheritDataAwareIterator($this->children))); | ||
} | ||
|
||
if ($dispatcher->hasListeners(FormEvents::POST_SET_DATA)) { | ||
$event = new PostSetDataEvent($this, $modelData); | ||
$dispatcher->dispatch($event, FormEvents::POST_SET_DATA); | ||
foreach ($thisModelDataAwareFormsIterator as $form) { | ||
$dispatcher = $form->getConfig()->getEventDispatcher(); | ||
if ($dispatcher->hasListeners(FormEvents::POST_SET_DATA)) { | ||
$event = new PostSetDataEvent($form, $modelData); | ||
$dispatcher->dispatch($event, FormEvents::POST_SET_DATA); | ||
} | ||
} | ||
|
||
return $this; | ||
|
@@ -465,7 +476,13 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static | |
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.'); | ||
} | ||
|
||
$dispatcher = $this->config->getEventDispatcher(); | ||
$thisModelDataAwareFormsIterator = new \AppendIterator(); | ||
|
||
// Collecting $this + all children with "inherit_data" option TRUE only if $this has "inherit_data" option FALSE to avoid to dispatch events two times. | ||
if (!$this->getConfig()->getInheritData()) { | ||
$thisModelDataAwareFormsIterator->append(new \ArrayIterator(['' => $this])); | ||
$thisModelDataAwareFormsIterator->append(new InheritDataAwareFilter(new InheritDataAwareIterator($this->children))); | ||
} | ||
|
||
$modelData = null; | ||
$normData = null; | ||
|
@@ -477,10 +494,19 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static | |
} | ||
|
||
// Hook to change content of the data submitted by the browser | ||
if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) { | ||
$event = new PreSubmitEvent($this, $submittedData); | ||
$dispatcher->dispatch($event, FormEvents::PRE_SUBMIT); | ||
$submittedData = $event->getData(); | ||
foreach ($thisModelDataAwareFormsIterator as $name => $form) { | ||
$dispatcher = $form->getConfig()->getEventDispatcher(); | ||
if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) { | ||
$eventSubmittedData = null; | ||
if (empty($name)) { | ||
$eventSubmittedData = $submittedData; | ||
} elseif (\array_key_exists($name, $submittedData)) { | ||
$eventSubmittedData = $submittedData[$name]; | ||
} | ||
$event = new PreSubmitEvent($form, $eventSubmittedData); | ||
$dispatcher->dispatch($event, FormEvents::PRE_SUBMIT); | ||
$submittedData = $event->getData(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is broken. It will replace the whole submitted data by the data of the child only. |
||
} | ||
} | ||
|
||
// Check whether the form is compound. | ||
|
@@ -563,10 +589,13 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static | |
|
||
// Hook to change content of the data in the normalized | ||
// representation | ||
if ($dispatcher->hasListeners(FormEvents::SUBMIT)) { | ||
$event = new SubmitEvent($this, $normData); | ||
$dispatcher->dispatch($event, FormEvents::SUBMIT); | ||
$normData = $event->getData(); | ||
foreach ($thisModelDataAwareFormsIterator as $form) { | ||
$dispatcher = $form->getConfig()->getEventDispatcher(); | ||
if ($dispatcher->hasListeners(FormEvents::SUBMIT)) { | ||
$event = new SubmitEvent($form, $normData); | ||
$dispatcher->dispatch($event, FormEvents::SUBMIT); | ||
$normData = $event->getData(); | ||
} | ||
} | ||
|
||
// Synchronize representations - must not change the content! | ||
|
@@ -590,9 +619,12 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static | |
$this->normData = $normData; | ||
$this->viewData = $viewData; | ||
|
||
if ($dispatcher->hasListeners(FormEvents::POST_SUBMIT)) { | ||
$event = new PostSubmitEvent($this, $viewData); | ||
$dispatcher->dispatch($event, FormEvents::POST_SUBMIT); | ||
foreach ($thisModelDataAwareFormsIterator as $form) { | ||
$dispatcher = $form->getConfig()->getEventDispatcher(); | ||
if ($dispatcher->hasListeners(FormEvents::POST_SUBMIT)) { | ||
$event = new PostSubmitEvent($form, $viewData); | ||
$dispatcher->dispatch($event, FormEvents::POST_SUBMIT); | ||
} | ||
} | ||
|
||
return $this; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Form/Util/InheritDataAwareFilter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* @author Cristoforo Cervino <cristoforo.cervino@me.com> | ||
*/ | ||
namespace Symfony\Component\Form\Util; | ||
|
||
class InheritDataAwareFilter extends \FilterIterator | ||
{ | ||
public function accept(): bool | ||
{ | ||
return (bool) $this->current()->getConfig()->getInheritData(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would not allow child forms with inherit_data to change the model data of the parent form. This is a very bad idea IMO (and was one of the reasons why this was not supported)