-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add MultiStepType
#59548
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
[Form] Add MultiStepType
#59548
Changes from 1 commit
e83ec2a
3f74429
bc77606
92c89e6
1befa38
d6e3215
5771ee6
21780e4
9025a65
865288b
a71ee28
a23b651
5dba331
8784a81
0f0b645
4f677b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
|
@@ -29,15 +30,31 @@ | |
{ | ||
$resolver | ||
->setRequired('steps') | ||
->setDefault('current_step', static function (Options $options): string { | ||
/** @var array<string, mixed> $steps */ | ||
$steps = $options['steps']; | ||
$firstStep = array_key_first($steps); | ||
if (!\is_string($firstStep)) { | ||
throw new \InvalidArgumentException('The option "steps" must be an associative array.'); | ||
->setAllowedTypes('steps', 'array') | ||
There was a problem 10000 hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. Should all current_step / next_step etc allow callable ? That would ease integration with Stepper or Navigator or any future WizardStepPathFinder-like.. wdyt ? 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. Good idea but i dont know what i should do here. Maybe i dont fully understand the approach |
||
->setAllowedValues('steps', static function (array $steps): bool { | ||
foreach ($steps as $key => $step) { | ||
if (!\is_string($key)) { | ||
return false; | ||
} | ||
|
||
if ((!\is_string($step) || !\is_subclass_of($step, AbstractType::class)) && !\is_callable($step)) { | ||
return false; | ||
} | ||
} | ||
|
||
return $firstStep; | ||
return true; | ||
}) | ||
->setRequired('current_step') | ||
->setAllowedTypes('current_step', 'string') | ||
->setNormalizer('current_step', static function (Options $options, string $value): string { | ||
if (!\array_key_exists($value, $options['steps'])) { | ||
throw new InvalidOptionsException(\sprintf('The current step "%s" does not exist.', $value)); | ||
} | ||
|
||
return $value; | ||
}) | ||
->setDefault('current_step', static function (Options $options): string { | ||
Check failure on line 56 in src/Symfony/Component/Form/Extension/Core/Type/MultiStepType.php
|
||
return \array_key_first($options['steps']); | ||
Check failure on line 57 in src/Symfony/Component/Form/Extension/Core/Type/MultiStepType.php
|
||
}); | ||
} | ||
|
||
|
@@ -48,21 +65,23 @@ | |
if (\is_callable($currentStep)) { | ||
$currentStep($builder, $options); | ||
} elseif (\is_string($currentStep)) { | ||
if (!class_exists($currentStep)) { | ||
throw new \InvalidArgumentException(\sprintf('The form class "%s" does not exist.', $currentStep)); | ||
} | ||
|
||
if (!is_subclass_of($currentStep, AbstractType::class)) { | ||
throw new \InvalidArgumentException(\sprintf('"%s" is not a form type.', $currentStep)); | ||
} | ||
|
||
$builder->add($options['current_step'], $currentStep); | ||
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. the step type may require specific options that differ from those in the root form. we need to consider this, as it's currently a limitation |
||
} | ||
} | ||
|
||
public function buildView(FormView $view, FormInterface $form, array $options): void | ||
{ | ||
$view->vars['current_step'] = $options['current_step']; | ||
$view->vars['steps'] = array_keys($options['steps']); | ||
$view->vars['steps'] = \array_keys($options['steps']); | ||
$view->vars['total_steps_count'] = \count($options['steps']); | ||
|
||
/** @var int $currentStepIndex */ | ||
$currentStepIndex = \array_search($options['current_step'], \array_keys($options['steps']), true); | ||
$view->vars['current_step_number'] = $currentStepIndex + 1; | ||
$view->vars['is_first_step'] = $currentStepIndex === 0; | ||
|
||
/** @var int $lastStepIndex */ | ||
$lastStepIndex = \array_key_last(\array_keys($options['steps'])); | ||
$view->vars['is_last_step'] = $lastStepIndex === $currentStepIndex; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.