diff --git a/composer.json b/composer.json index bc5f556..692d509 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "phpstan/phpstan-strict-rules": "^0.12", "webchemistry/testing-helpers": "^3.0.0" }, - "minimum-stability": "dev", + "minimum-stability": "stable", "autoload": { "psr-4": { "Contributte\\FormWizard\\": [ diff --git a/src/Facade.php b/src/Facade.php index 7622856..9cb5b72 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -3,6 +3,8 @@ namespace Contributte\FormWizard; use Closure; +use LogicException; +use Nette\Application\UI\Form as UIForm; use Nette\Forms\Form; use Nette\SmartObject; use Nette\Utils\ArrayHash; @@ -108,6 +110,29 @@ public function isDisabled(int $step): bool return !$this->wizard->getSteps()[$step]; } + public function attached(): void + { + if ($this->wizard instanceof Wizard) { + $this->wizard->callStartup(); + } + + $form = $this->wizard->create(null, false); + + if ($form instanceof UIForm) { + throw new LogicException(sprintf('Cannot call this method on %s.', UIForm::class)); + } + + $submitted = (bool) $form->isSubmitted(); + if ($submitted) { + $form->fireEvents(); + } + } + + public function renderCurrentComponent(): string + { + return (string) $this->getCurrentComponent(); + } + /** * @return mixed */ diff --git a/src/IWizard.php b/src/IWizard.php index 972df4a..56820f4 100644 --- a/src/IWizard.php +++ b/src/IWizard.php @@ -2,7 +2,7 @@ namespace Contributte\FormWizard; -use Nette\Application\UI\Form; +use Nette\Forms\Form; use Nette\Utils\ArrayHash; interface IWizard @@ -26,7 +26,7 @@ public function render(): void; public function reset(): void; - public function create(?string $step = null): Form; + public function create(?string $step = null, bool $defaultValues = true): Form; public function isSuccess(): bool; diff --git a/src/Wizard.php b/src/Wizard.php index eb027ab..db6e314 100644 --- a/src/Wizard.php +++ b/src/Wizard.php @@ -120,9 +120,9 @@ protected function getSection(): WizardSessionSection public function getStepCounter(): StepCounter { - $counter = 1; if ($this->stepCounter === null) { - for ($counter = 1; $counter < 1000; $counter++) { + $counter = 1; + for (; $counter < 1000; $counter++) { if (!method_exists($this, 'createStep' . $counter) && !$this->getComponent('step' . $counter, false)) { $counter--; break; @@ -278,7 +278,7 @@ public function reset(): void $this->stepCounter = null; } - public function create(?string $step = null): Form + public function create(?string $step = null, bool $defaultValues = true): Forms\Form { $step = (int) ($step ?? $this->getCurrentStep()); /** @var Form $form */ @@ -291,7 +291,9 @@ public function create(?string $step = null): Form } // Set submited values - $form->setValues((array) $this->getSection()->getStepValues($step)); + if ($defaultValues) { + $form->setValues((array) $this->getSection()->getStepValues($step)); + } return $form; } @@ -380,7 +382,10 @@ public function getPresenter(): ?Presenter return $this->presenter; } - protected function validateParent(IContainer $parent): void + /** + * @internal only for standalone usage, use Facade instead of + */ + public function callStartup(): void { if (!$this->startupCalled) { $this->startupCalled = true; @@ -389,4 +394,9 @@ protected function validateParent(IContainer $parent): void } } + protected function validateParent(IContainer $parent): void + { + $this->callStartup(); + } + }