-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTestPresenter.php
134 lines (96 loc) · 3.07 KB
/
TestPresenter.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types = 1);
namespace App\Presenters;
use Nette\Application\UI\Form;
use Nette\Application\UI\Presenter;
use Nette\Application\UI\Multiplier;
final class TestPresenter extends Presenter
{
// === NON-AJAX FORMS ========================================
protected function createComponentBasicForm(): Form
{
$form = static::factoryForm();
$form->onSuccess[] = [$this, 'onFormSuccess'];
return $form;
}
protected function createComponentMultiForm(): Multiplier
{
return new Multiplier(function ($name) {
$form = static::factoryForm();
$form->onSuccess[] = [$this, 'onFormSuccess'];
return $form;
});
}
public function onFormSuccess(Form $form): void
{
$this->flashMessage(sprintf('reCAPTCHA has been validated successfully! (form "%s")', $form->getName()), 'success');
$this->redirect('this');
}
// === AJAX FORMS ========================================
protected function createComponentAjaxForm(): Form
{
$form = static::factoryForm();
// redraw form on each submit due to possible errors
$form->onSubmit[] = function () {
$this->redrawControl('form');
};
$form->onSuccess[] = [$this, 'onAjaxFormSuccess'];
return $form;
}
protected function createComponentMultiAjaxForm(): Multiplier
{
return new Multiplier(function ($name) {
$form = static::factoryForm();
$form->onSubmit[] = function () use ($name) {
// redraw form snippet just like in single AJAX form example
$this->redrawControl('form-' . $name);
};
$form->onSuccess[] = [$this, 'onAjaxFormSuccess'];
return $form;
});
}
public function onAjaxFormSuccess(Form $form): void
{
$this->flashMessage(sprintf('reCAPTCHA has been validated successfully! (form "%s")', $form->getName()), 'success');
$this->redrawControl('flashes');
if (!$this->isAjax()) {
$this->flashMessage('Non-AJAX request detected. The page has therefore been reloaded.', 'info');
$this->redirect('this');
}
}
// === AJAX FORMS ========================================
protected function createComponentInvisibleForm(): Form
{
$form = static::factoryInvisibleForm();
$form->onSuccess[] = [$this, 'onFormSuccess'];
return $form;
}
protected function createComponentMultiInvisibleForm(): Multiplier
{
return new Multiplier(function ($name) {
$form = static::factoryInvisibleForm();
$form->onSuccess[] = [$this, 'onFormSuccess'];
return $form;
});
}
// === FORM FACTORY ========================================
private static function factoryForm(): Form
{
$form = new Form;
$form->addReCaptcha('recaptcha', 'reCAPTCHA for you', "Please prove you're not a robot.");
$form->addSubmit('send', 'Submit form');
return $form;
}
private static function factoryInvisibleForm(): Form
{
$form = new Form;
$form->addText('email', 'Your e-mail')
->setHtmlType('email')
->setEmptyValue('@')
->setRequired(true)
->addRule(Form::EMAIL, 'Please provide a valid e-mail address.');
$form->addReCaptcha('recaptcha', null, "Please prove you're not a robot.");
$form->addSubmit('send', 'Submit form');
return $form;
}
}