Skip to content

Commit 00b8d61

Browse files
committed
Mark FormFlow as finished if the last step is skipped
1 parent fd1752e commit 00b8d61

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

Flow/FormFlow.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ private function move(\Closure $direction): bool
221221
if (!$this->config->getStep($newStep)->isSkipped($data)) {
222222
break;
223223
}
224+
225+
if ($cursor->isLastStep()) {
226+
$this->finished = true;
227+
228+
if ($this->config->isAutoReset()) {
229+
$this->reset();
230+
231+
return true;
232+
}
233+
234+
break;
235+
}
224236
}
225237

226238
$this->cursor = $cursor;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures\Flow;
13+
14+
use Symfony\Component\Form\Extension\Core\Type\TextType;
15+
use Symfony\Component\Form\Flow\AbstractFlowType;
16+
use Symfony\Component\Form\Flow\FormFlowBuilderInterface;
17+
use Symfony\Component\Form\Flow\Type\NavigatorFlowType;
18+
use Symfony\Component\OptionsResolver\OptionsResolver;
19+
20+
class LastStepSkippedType extends AbstractFlowType
21+
{
22+
public function buildFormFlow(FormFlowBuilderInterface $builder, array $options): void
23+
{
24+
$builder->addStep('step1', TextType::class);
25+
$builder->addStep('step2', skip: static fn () => true);
26+
27+
$builder->add('navigator', NavigatorFlowType::class);
28+
}
29+
30+
public function configureOptions(OptionsResolver $resolver): void
31+
{
32+
$resolver->setDefaults([
33+
'data_class' => null,
34+
'step_property_path' => '[currentStep]',
35+
]);
36+
}
37+
}

Tests/Flow/FormFlowTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Form\Forms;
2727
use Symfony\Component\Form\Tests\Fixtures\Flow\Data\UserSignUp;
2828
use Symfony\Component\Form\Tests\Fixtures\Flow\Extension\UserSignUpTypeExtension;
29+
use Symfony\Component\Form\Tests\Fixtures\Flow\LastStepSkippedType;
2930
use Symfony\Component\Form\Tests\Fixtures\Flow\UserSignUpType;
3031
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
3132
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
@@ -932,4 +933,40 @@ public function testStepValidationGroups()
932933
// Check that validation groups are updated
933934
self::assertEquals(['Default', 'professional'], $flow->getConfig()->getOption('validation_groups')($flow));
934935
}
936+
937+
public function testLastStepSkippedMarkFlowAsFinished()
938+
{
939+
$flow = $this->factory->create(LastStepSkippedType::class, ['currentStep' => 'step1']);
940+
941+
self::assertSame('step1', $flow->getCursor()->getCurrentStep());
942+
self::assertFalse($flow->isSubmitted());
943+
self::assertNull($flow->getClickedButton());
944+
self::assertTrue($flow->has('step1'));
945+
self::assertTrue($flow->has('navigator'));
946+
947+
$navigatorForm = $flow->get('navigator');
948+
self::assertCount(1, $navigatorForm->all());
949+
self::assertTrue($navigatorForm->has('next'));
950+
951+
$flow->submit([
952+
'step1' => 'foo',
953+
'navigator' => [
954+
'next' => '',
955+
],
956+
]);
957+
958+
self::assertSame('step1', $flow->getCursor()->getCurrentStep());
959+
self::assertTrue($flow->isSubmitted());
960+
self::assertTrue($flow->isValid());
961+
self::assertFalse($flow->isFinished());
962+
self::assertNotNull($button = $flow->getClickedButton());
963+
self::assertTrue($button->isNextAction());
964+
self::assertTrue($button->isClicked());
965+
966+
$button->handle(); // $flow->moveNext() is called internally
967+
968+
self::assertTrue($flow->isFinished());
969+
self::assertNotSame($flow, $flow->getStepForm());
970+
self::assertSame(['currentStep' => 'step1', 'step1' => 'foo'], $flow->getData());
971+
}
935972
}

0 commit comments

Comments
 (0)