-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Form] Add FormFlow
for multistep forms management
#60212
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
yceruto
wants to merge
1
commit into
symfony:7.4
Choose a base branch
from
yceruto:formflow
base: 7.4
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.
+3,470
−3
Open
Changes from all commits
Commits
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
Add FormFlow for multistep forms management
- Loading branch information
commit 92ee39c3c626ce2d97c0b179cbbc95893d6ae49c
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
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
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
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
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
44 changes: 44 additions & 0 deletions
44
.../Component/Form/Extension/HttpFoundation/Type/FormFlowTypeSessionDataStorageExtension.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,44 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\HttpFoundation\Type; | ||
|
||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\Flow\DataStorage\SessionDataStorage; | ||
use Symfony\Component\Form\Flow\FormFlowBuilderInterface; | ||
use Symfony\Component\Form\Flow\Type\FormFlowType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class FormFlowTypeSessionDataStorageExtension extends AbstractTypeExtension | ||
{ | ||
public function __construct( | ||
private readonly ?RequestStack $requestStack = null, | ||
) { | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
\assert($builder instanceof FormFlowBuilderInterface); | ||
|
||
if (null === $this->requestStack || null !== $options['data_storage']) { | ||
return; | ||
} | ||
|
||
$key = \sprintf('_sf_formflow.%s_%s', strtolower(str_replace('\\', '_', $builder->getType()->getInnerType()::class)), $builder->getName()); | ||
$builder->setDataStorage(new SessionDataStorage($key, $this->requestStack)); | ||
} | ||
|
||
public static function getExtendedTypes(): iterable | ||
{ | ||
return [FormFlowType::class]; | ||
} | ||
} |
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,38 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Flow; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Flow\Type\FormFlowType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
/** | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
abstract class AbstractFlowType extends AbstractType implements FormFlowTypeInterface | ||
{ | ||
final public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
\assert($builder instanceof FormFlowBuilderInterface); | ||
|
||
$this->buildFormFlow($builder, $options); | ||
} | ||
|
||
public function buildFormFlow(FormFlowBuilderInterface $builder, array $options): void | ||
{ | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return FormFlowType::class; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Symfony/Component/Form/Flow/DataStorage/DataStorageInterface.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,26 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Flow\DataStorage; | ||
|
||
/** | ||
* Handles storing and retrieving form data between steps. | ||
* | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
interface DataStorageInterface | ||
{ | ||
public function save(object|array $data): void; | ||
|
||
public function load(object|array|null $default = null): object|array|null; | ||
|
||
public function clear(): void; | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Symfony/Component/Form/Flow/DataStorage/InMemoryDataStorage.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,40 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Flow\DataStorage; | ||
|
||
/** | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
class InMemoryDataStorage implements DataStorageInterface | ||
{ | ||
private array $memory = []; | ||
|
||
public function __construct( | ||
private readonly string $key, | ||
) { | ||
} | ||
|
||
public function save(object|array $data): void | ||
{ | ||
$this->memory[$this->key] = $data; | ||
} | ||
|
||
public function load(object|array|null $default = null): object|array|null | ||
{ | ||
return $this->memory[$this->key] ?? $default; | ||
} | ||
|
||
public function clear(): void | ||
{ | ||
unset($this->memory[$this->key]); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Form/Flow/DataStorage/NullDataStorage.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,33 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Flow\DataStorage; | ||
|
||
/** | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
final class NullDataStorage implements DataStorageInterface | ||
{ | ||
public function save(object|array $data): void | ||
{ | ||
// no-op | ||
} | ||
|
||
public function load(object|array|null $default = null): object|array|null | ||
{ | ||
return $default; | ||
} | ||
|
||
public function clear(): void | ||
{ | ||
// no-op | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/Form/Flow/DataStorage/SessionDataStorage.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,41 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Flow\DataStorage; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
class SessionDataStorage implements DataStorageInterface | ||
{ | ||
public function __construct( | ||
private readonly string $key, | ||
private readonly RequestStack $requestStack, | ||
) { | ||
} | ||
|
||
public function save(object|array $data): void | ||
{ | ||
$this->requestStack->getSession()->set($this->key, $data); | ||
} | ||
|
||
public function load(object|array|null $default = null): object|array|null | ||
{ | ||
return $this->requestStack->getSession()->get($this->key, $default); | ||
} | ||
|
||
public function clear(): void | ||
{ | ||
$this->requestStack->getSession()->remove($this->key); | ||
} | ||
} |
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,92 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Flow; | ||
|
||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\SubmitButton; | ||
|
||
/** | ||
* A button that submits the form and handles an action. | ||
* | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
class FlowButton extends SubmitButton implements FlowButtonInterface | ||
{ | ||
private mixed $data = null; | ||
private bool $handled = false; | ||
|
||
public function submit(array|string|null $submittedData, bool $clearMissing = true): static | ||
{ | ||
if ($this->isSubmitted()) { | ||
return $this; // ignore double submit | ||
} | ||
|
||
parent::submit($submittedData, $clearMissing); | ||
|
||
if ($this->isSubmitted()) { | ||
$this->data = $submittedData; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function getViewData(): mixed | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
/** @var FormInterface $form */ | ||
$form = $this->getParent(); | ||
$data = $form->getData(); | ||
|
||
while ($form && !$form instanceof FormFlowInterface) { | ||
$form = $form->getParent(); | ||
} | ||
|
||
$handler = $this->getConfig()->getOption('handler'); | ||
$handler($data, $this, $form); | ||
|
||
$this->handled = true; | ||
} | ||
|
||
public function isHandled(): bool | ||
{ | ||
return $this->handled; | ||
} | ||
|
||
public function isResetAction(): bool | ||
{ | ||
return 'reset' === $this->getConfig()->getAttribute('action'); | ||
} | ||
|
||
public function isPreviousAction(): bool | ||
{ | ||
return 'previous' === $this->getConfig()->getAttribute('action'); | ||
} | ||
|
||
public function isNextAction(): bool | ||
{ | ||
return 'next' === $this->getConfig()->getAttribute('action'); | ||
} | ||
|
||
public function isFinishAction(): bool | ||
{ | ||
return 'finish' === $this->getConfig()->getAttribute('action'); | ||
} | ||
|
||
public function isClearSubmission(): bool | ||
{ | ||
return $this->getConfig()->getOption('clear_submission'); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.