# Contributte\Form-wizzard

# Content

# Usage

# Register extension

extensions:
	- Contributte\FormWizard\DI\WizardExtension
1
2

# Component


use Nette\Application\UI\Form;

class Wizard extends Contributte\FormWizard\Wizard {

	private array $stepNames = [
	    1 => "Skip username",
	    2 => "Username",
	    3 => "Email",
	];

	protected function finish(): void
	{
		$values = $this->getValues();
	}

	protected function startup(): void
	{
		$this->skipStepIf(2, function (array $values): bool {
			return isset($values[1]) && $values[1]['skip'] === true;
		});
		$this->setDefaultValues(2, function (Form $form, array $values) {
			$data = [
			    'username' => 'john_doe'
			];
			$form->setDefaults($data);
		});
	}

	public function getStepData(int $step): array
	{
		return [
		    'name' => $this->stepNames[$step]
		];
	}

	protected function createStep1(): Form
	{
		$form = $this->createForm();

		$form->addCheckbox('skip', 'Skip username');

		$form->addSubmit(self::NEXT_SUBMIT_NAME, 'Next');

		return $form;
	}

	protected function createStep2(): Form
	{
		$form = $this->createForm();

		$form->addText('username', 'Username')
			->setRequired();

		$form->addSubmit(self::PREV_SUBMIT_NAME, 'Back');
		$form->addSubmit(self::NEXT_SUBMIT_NAME, 'Next');

		return $form;
	}

	protected function createStep3(): Form
	{
		$form = $this->createForm();

		$form->addText('email', 'Email')
			->setRequired();

		$form->addSubmit(self::PREV_SUBMIT_NAME, 'Back');
		$form->addSubmit(self::FINISH_SUBMIT_NAME, 'Register');

		return $form;
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
services:
	- Wizard
1
2

# Presenter


final class HomepagePresenter extends Nette\Application\UI\Presenter {

	/** @var Wizard @inject */
	public $wizard;
	
	public function handleChangeStep(int $step): void 
	{
		$this['wizard']->setStep($step);
		
		$this->redirect('wizard'); // Optional, hides parameter from URL
	}

	protected function createComponentWizard(): Wizard 
	{
		return $this->wizard;
	}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# Template

<div n:wizard="wizard">
    <ul n:if="!$wizard->isSuccess()">
        <li n:foreach="$wizard->steps as $step" n:class="$wizard->isDisabled($step) ? disabled, $wizard->isActive($step) ? active">
            <a n:tag-if="$wizard->useLink($step)" n:href="changeStep! $step">{$step} - {$wizard->getStepData($step)['name']}</a>
        </li>
    </ul>

    {step 1}
        {control $form}
    {/step}

    {step 2}
        {control $form}
    {/step}

    {step 3}
        {control $form}
    {/step}

    {step success}
        Registration was successful
    {/step}
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23