-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.4 | ||
--- | ||
|
||
* Add `FormFlow` component for multistep forms management | ||
|
||
7.3 | ||
--- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?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\Core\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Flow\ActionButtonInterface; | ||
use Symfony\Component\Form\Flow\ActionButtonTypeInterface; | ||
use Symfony\Component\Form\Flow\FormFlowCursor; | ||
use Symfony\Component\Form\Flow\FormFlowInterface; | ||
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* An action-based submit button for a form flow. | ||
* | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
class FormFlowActionType extends AbstractType implements ActionButtonTypeInterface | ||
{ | ||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->define('action') | ||
->info('The action name of the button') | ||
->default('') | ||
->allowedTypes('string'); | ||
|
||
$resolver->define('handler') | ||
->info('A callable that will be called when this button is clicked') | ||
->default(function (Options $options) { | ||
if (!\in_array($options['action'], ['back', 'next', 'finish', 'reset'], true)) { | ||
throw new MissingOptionsException(\sprintf('The option "handler" is required for the action "%s".', $options['action'])); | ||
} | ||
|
||
return function (mixed $data, ActionButtonInterface $button, FormFlowInterface $flow): void { | ||
match (true) { | ||
$button->isBackAction() => $flow->moveBack($button->getViewData()), | ||
$button->isNextAction() => $flow->moveNext(), | ||
$button->isFinishAction(), $button->isResetAction() => $flow->reset(), | ||
Check failure on line 48 in src/Symfony/Component/Form/Extension/Core/Type/FormFlowActionType.php
|
||
}; | ||
}; | ||
}) | ||
->allowedTypes('callable'); | ||
|
||
$resolver->define('include_if') | ||
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. nice 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. I've improved it a bit more for better DX, so now we have three allowed values:
|
||
->info('Decide whether to include this button in the current form') | ||
->default(function (Options $options) { | ||
return match ($options['action']) { | ||
'back' => fn (FormFlowCursor $cursor): bool => $cursor->canMoveBack(), | ||
'next' => fn (FormFlowCursor $cursor): bool => $cursor->canMoveNext(), | ||
'finish' => fn (FormFlowCursor $cursor): bool => $cursor->isLastStep(), | ||
default => null, | ||
}; | ||
}) | ||
->allowedTypes('null', 'array', 'callable') | ||
->normalize(function (Options $options, mixed $value) { | ||
if (\is_array($value)) { | ||
return fn (FormFlowCursor $cursor): bool => \in_array($cursor->getCurrentStep(), $value, true); | ||
} | ||
|
||
return $value; | ||
}); | ||
|
||
$resolver->define('clear_submission') | ||
->info('Whether the submitted data will be cleared when this button is clicked') | ||
->default(function (Options $options) { | ||
return 'reset' === $options['action'] || 'back' === $options['action']; | ||
}) | ||
->allowedTypes('bool'); | ||
|
||
$resolver->setDefault('validate', function (Options $options) { | ||
return !$options['clear_submission']; | ||
}); | ||
|
||
$resolver->setDefault('validation_groups', function (Options $options) { | ||
return $options['clear_submission'] ? false : null; | ||
}); | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return SubmitType::class; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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\Core\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* A navigator type that defines default actions to interact with a form flow. | ||
* | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
class FormFlowNavigatorType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder->add('back', FormFlowActionType::class, [ | ||
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. what bout "previous" ? generally it goes with "next" 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. I don’t have a strong opinion on that. I personally prefer the shorter version, and users can change it using the |
||
'action' => 'back', | ||
]); | ||
|
||
$builder->add('next', FormFlowActionType::class, [ | ||
'action' => 'next', | ||
]); | ||
|
||
$builder->add('finish', FormFlowActionType::class, [ | ||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'action' => 'finish', | ||
]); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'label' => false, | ||
'mapped' => false, | ||
'priority' => -100, | ||
]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?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\Core\Type; | ||
|
||
use Symfony\Component\Form\Flow\AbstractFlowType; | ||
use Symfony\Component\Form\Flow\DataAccessor\PropertyPathStepAccessor; | ||
use Symfony\Component\Form\Flow\DataAccessor\StepAccessorInterface; | ||
use Symfony\Component\Form\Flow\DataStorage\DataStorageInterface; | ||
use Symfony\Component\Form\Flow\DataStorage\NullDataStorage; | ||
use Symfony\Component\Form\Flow\FormFlowInterface; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
use Symfony\Component\PropertyAccess\PropertyPath; | ||
use Symfony\Component\PropertyAccess\PropertyPathInterface; | ||
|
||
/** | ||
* A multistep form. | ||
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. a question about naming: you describe this as a form multistep its feels weird at first read 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. They mean the same to me... I’m just phrasing it differently to make the idea clearer. What is the weird part? |
||
* | ||
* @author Yonel Ceruto <open@yceruto.dev> | ||
*/ | ||
class FormFlowType extends AbstractFlowType | ||
{ | ||
public function __construct( | ||
private ?PropertyAccessorInterface $propertyAccessor = null, | ||
) { | ||
$this->propertyAccessor ??= PropertyAccess::createPropertyAccessor(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder->setDataStorage($options['data_storage'] ?? new NullDataStorage()); | ||
$builder->setStepAccessor($options['step_accessor']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildView(FormView $view, FormInterface $form, array $options): void | ||
{ | ||
$view->vars['cursor'] = $cursor = $form->getCursor(); | ||
|
||
$index = 0; | ||
$position = 1; | ||
foreach ($form->getConfig()->getSteps() as $name => $step) { | ||
$isSkipped = $step->isSkipped($form->getViewData()); | ||
|
||
$stepVars = [ | ||
'name' => $name, | ||
'index' => $index++, | ||
'position' => $isSkipped ? -1 : $position++, | ||
'is_current_step' => $name === $cursor->getCurrentStep(), | ||
'can_be_skipped' => null !== $step->getSkip(), | ||
'is_skipped' => $isSkipped, | ||
]; | ||
|
||
$view->vars['steps'][$name] = $stepVars; | ||
|
||
if (!$isSkipped) { | ||
$view->vars['visible_steps'][$name] = $stepVars; | ||
} | ||
} | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->define('data_storage') | ||
->default(null) | ||
->allowedTypes('null', DataStorageInterface::class); | ||
|
||
$resolver->define('step_accessor') | ||
->default(function (Options $options) { | ||
if (!isset($options['step_property_path'])) { | ||
throw new MissingOptionsException('Option "step_property_path" is required.'); | ||
} | ||
|
||
return new PropertyPathStepAccessor($this->propertyAccessor, $options['step_property_path']); | ||
}) | ||
->allowedTypes(StepAccessorInterface::class); | ||
|
||
$resolver->define('step_property_path') | ||
->info('Required if the default step_accessor is being used') | ||
->allowedTypes('string', PropertyPathInterface::class) | ||
->normalize(function (Options $options, string|PropertyPathInterface $value): PropertyPathInterface { | ||
return \is_string($value) ? new PropertyPath($value) : $value; | ||
}); | ||
|
||
$resolver->define('auto_reset') | ||
->info('Whether the FormFlow will be reset automatically when it is finished') | ||
->default(true) | ||
->allowedTypes('bool'); | ||
|
||
$resolver->setDefault('validation_groups', function (FormFlowInterface $flow) { | ||
return ['Default', $flow->getCursor()->getCurrentStep()]; | ||
}); | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return FormType::class; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?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\Extension\Core\Type\FormFlowType; | ||
use Symfony\Component\Form\Flow\DataStorage\SessionDataStorage; | ||
use Symfony\Component\Form\Flow\FormFlowBuilderInterface; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class FormFlowTypeSessionDataStorageExtension extends AbstractTypeExtension | ||
{ | ||
public function __construct( | ||
private readonly ?RequestStack $requestStack = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @param FormFlowBuilderInterface $builder | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
Check failure on line 31 in src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormFlowTypeSessionDataStorageExtension.php
|
||
{ | ||
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 | ||
Check failure on line 41 in src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormFlowTypeSessionDataStorageExtension.php
|
||
{ | ||
yield FormFlowType::class; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.