Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
silasjoisten committed Dec 7, 2024
1 parent 0d280f9 commit 20ef568
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
29 changes: 15 additions & 14 deletions src/LiveComponent/src/ComponentWithMultiStepFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\UX\LiveComponent\Storage\StorageInterface;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
use Symfony\UX\TwigComponent\Attribute\PostMount;

use function Symfony\Component\String\u;

/**
Expand All @@ -29,8 +30,8 @@
*/
trait ComponentWithMultiStepFormTrait
{
use DefaultActionTrait;
use ComponentWithFormTrait;
use DefaultActionTrait;

#[LiveProp]
public ?string $currentStepName = null;
Expand All @@ -49,19 +50,19 @@ public function hasValidationErrors(): bool
/**
* @internal
*
* Must be executed after ComponentWithFormTrait::initializeForm().
* Must be executed after ComponentWithFormTrait::initializeForm()
*/
#[PostMount(priority: -250)]
public function initialize(): void
{
$this->currentStepName = $this->getStorage()->get(
sprintf('%s_current_step_name', self::prefix()),
\sprintf('%s_current_step_name', self::prefix()),
$this->formView->vars['current_step_name'],
);

$this->form = $this->instantiateForm();

$formData = $this->getStorage()->get(sprintf(
$formData = $this->getStorage()->get(\sprintf(
'%s_form_values_%s',
self::prefix(),
$this->currentStepName,
Expand Down Expand Up @@ -91,7 +92,7 @@ public function next(): void
}

$this->getStorage()->persist(
sprintf('%s_form_values_%s', self::prefix(), $this->currentStepName),
\sprintf('%s_form_values_%s', self::prefix(), $this->currentStepName),
$this->form->getData(),
);

Expand All @@ -117,13 +118,13 @@ public function next(): void
}

$this->currentStepName = $next;
$this->getStorage()->persist(sprintf('%s_current_step_name', self::prefix()), $this->currentStepName);
$this->getStorage()->persist(\sprintf('%s_current_step_name', self::prefix()), $this->currentStepName);

// If we have a next step, we need to resinstantiate the form and reset the form view and values.
$this->form = $this->instantiateForm();
$this->formView = null;

$formData = $this->getStorage()->get(sprintf(
$formData = $this->getStorage()->get(\sprintf(
'%s_form_values_%s',
self::prefix(),
$this->currentStepName,
Expand Down Expand Up @@ -165,12 +166,12 @@ public function previous(): void
}

$this->currentStepName = $previous;
$this->getStorage()->persist(sprintf('%s_current_step_name', self::prefix()), $this->currentStepName);
$this->getStorage()->persist(\sprintf('%s_current_step_name', self::prefix()), $this->currentStepName);

$this->form = $this->instantiateForm();
$this->formView = null;

$formData = $this->getStorage()->get(sprintf(
$formData = $this->getStorage()->get(\sprintf(
'%s_form_values_%s',
self::prefix(),
$this->currentStepName,
Expand Down Expand Up @@ -202,7 +203,7 @@ public function submit(): void
}

$this->getStorage()->persist(
sprintf('%s_form_values_%s', self::prefix(), $this->currentStepName),
\sprintf('%s_form_values_%s', self::prefix(), $this->currentStepName),
$this->form->getData(),
);

Expand All @@ -219,7 +220,7 @@ public function getAllData(): array
$data = [];

foreach ($this->stepNames as $stepName) {
$data[$stepName] = $this->getStorage()->get(sprintf(
$data[$stepName] = $this->getStorage()->get(\sprintf(
'%s_form_values_%s',
self::prefix(),
$stepName,
Expand All @@ -232,12 +233,12 @@ public function getAllData(): array
public function resetForm(): void
{
foreach ($this->stepNames as $stepName) {
$this->getStorage()->remove(sprintf('%s_form_values_%s', self::prefix(), $stepName));
$this->getStorage()->remove(\sprintf('%s_form_values_%s', self::prefix(), $stepName));
}

$this->getStorage()->remove(sprintf('%s_current_step_name', self::prefix()));
$this->getStorage()->remove(\sprintf('%s_current_step_name', self::prefix()));

$this->currentStepName = $this->stepNames[\array_key_first($this->stepNames)];
$this->currentStepName = $this->stepNames[array_key_first($this->stepNames)];
$this->form = $this->instantiateForm();
$this->formView = null;
$this->formValues = $this->extractFormValues($this->getFormView());
Expand Down
9 changes: 5 additions & 4 deletions src/LiveComponent/src/Form/Type/MultiStepType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php


declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -9,8 +12,6 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Symfony\UX\LiveComponent\Form\Type;

use Symfony\Component\Form\AbstractType;
Expand All @@ -31,7 +32,7 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefault('current_step_name', static function (Options $options): string {
return \array_key_first($options['steps']);
return array_key_first($options['steps']);
})
->setRequired('steps');
}
Expand All @@ -44,6 +45,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['current_step_name'] = $options['current_step_name'];
$view->vars['steps_names'] = \array_keys($options['steps']);
$view->vars['steps_names'] = array_keys($options['steps']);
}
}
5 changes: 3 additions & 2 deletions src/LiveComponent/src/Storage/SessionStorage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php


declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -9,8 +12,6 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Symfony\UX\LiveComponent\Storage;

use Symfony\Component\HttpFoundation\RequestStack;
Expand Down
5 changes: 3 additions & 2 deletions src/LiveComponent/src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php


declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -9,8 +12,6 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Symfony\UX\LiveComponent\Storage;

/**
Expand Down

0 comments on commit 20ef568

Please sign in to comment.