Skip to content

Commit bf72864

Browse files
committed
Form: uses underscored signal parameter _do only in POST forms
Hidden field is created in receiveHttpData() because method can be altered after attached(), but not after HTTP data are loaded. Related to 647af4e
1 parent e2b4384 commit bf72864

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

src/Application/UI/Form.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,21 @@ protected function attached($presenter)
5959
{
6060
if ($presenter instanceof Presenter) {
6161
$name = $this->lookupPath(Presenter::class);
62-
6362
if (!isset($this->getElementPrototype()->id)) {
6463
$this->getElementPrototype()->id = 'frm-' . $name;
6564
}
65+
if (!$this->getAction()) {
66+
$this->setAction(new Link($presenter, $name . self::NAME_SEPARATOR . 'submit'));
67+
}
6668

67-
if (iterator_count($this->getControls()) && $this->isSubmitted()) {
68-
foreach ($this->getControls() as $control) {
69+
$controls = $this->getControls();
70+
if (iterator_count($controls) && $this->isSubmitted()) {
71+
foreach ($controls as $control) {
6972
if (!$control->isDisabled()) {
7073
$control->loadHttpData();
7174
}
7275
}
7376
}
74-
75-
if (!$this->getAction()) {
76-
$this->setAction(new Link($presenter, 'this', []));
77-
$signal = new Nette\Forms\Controls\HiddenField($name . self::NAME_SEPARATOR . 'submit');
78-
$signal->setOmitted()->setHtmlId(FALSE);
79-
$this['_' . Presenter::SIGNAL_KEY . '_'] = $signal;
80-
}
8177
}
8278
parent::attached($presenter);
8379
}
@@ -100,11 +96,20 @@ public function isAnchored()
10096
protected function receiveHttpData()
10197
{
10298
$presenter = $this->getPresenter();
99+
$isPost = $this->getMethod() === self::POST;
100+
101+
$action = $this->getAction();
102+
if ($action instanceof Link) {
103+
$this[($isPost ? '_' : '') . Presenter::SIGNAL_KEY] =
104+
(new Nette\Forms\Controls\HiddenField($action->getDestination()))
105+
->setOmitted()->setHtmlId(FALSE);
106+
$this->setAction(new Link($presenter, 'this'));
107+
}
108+
103109
if (!$presenter->isSignalReceiver($this, 'submit')) {
104110
return;
105111
}
106112

107-
$isPost = $this->getMethod() === self::POST;
108113
$request = $presenter->getRequest();
109114
if ($request->isMethod('forward') || $request->isMethod('post') !== $isPost) {
110115
return;

src/Application/UI/Link.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Link
3131
/**
3232
* Link specification.
3333
*/
34-
public function __construct(PresenterComponent $component, $destination, array $params)
34+
public function __construct(PresenterComponent $component, $destination, array $params = [])
3535
{
3636
$this->component = $component;
3737
$this->destination = $destination;

src/Application/UI/Presenter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ private function initGlobalParameters()
12121212
$selfParams = [];
12131213

12141214
$params = $this->request->getParameters();
1215-
if (($tmp = $this->request->getPost('_' . self::SIGNAL_KEY . '_')) !== NULL) {
1215+
if (($tmp = $this->request->getPost('_' . self::SIGNAL_KEY)) !== NULL) {
12161216
$params[self::SIGNAL_KEY] = $tmp;
12171217
} elseif ($this->isAjax()) {
12181218
$params += $this->request->getPost();

tests/UI/Presenter.parameters.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ test(function () {
7373
//_signal_ in POST
7474
$presenter = createPresenter();
7575
$presenter->run(new Application\Request('Foo', 'POST', [], [
76-
'_do_' => 'foo'
76+
'_do' => 'foo'
7777
]));
7878
Assert::same(['', 'foo'], $presenter->getSignal());
7979
});
@@ -91,7 +91,7 @@ test(function () {
9191
//_signal_ in POST overwriting GET
9292
$presenter = createPresenter();
9393
$presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [
94-
'_do_' => 'foo'
94+
'_do' => 'foo'
9595
]));
9696
Assert::same(['', 'foo'], $presenter->getSignal());
9797
});
@@ -121,7 +121,7 @@ test(function () {
121121
$presenter = createPresenter();
122122
$presenter->ajax = TRUE;
123123
$presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [
124-
'_do_' => 'foo'
124+
'_do' => 'foo'
125125
]));
126126
Assert::same(['', 'foo'], $presenter->getSignal());
127127
});

0 commit comments

Comments
 (0)