Skip to content

Commit be4baf6

Browse files
committed
Presenter: don't mix GET & POST params in AJAX when form is submitted [Closes nette/forms#33][Closes nette/nette#1061] (#127)
1 parent 76fdf3c commit be4baf6

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/Application/UI/Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function attached($presenter)
7676
$this->setAction(new Link($presenter, 'this', []));
7777
$signal = new Nette\Forms\Controls\HiddenField($name . self::NAME_SEPARATOR . 'submit');
7878
$signal->setOmitted()->setHtmlId(FALSE);
79-
$this[Presenter::SIGNAL_KEY] = $signal;
79+
$this['_' . Presenter::SIGNAL_KEY . '_'] = $signal;
8080
}
8181
}
8282
parent::attached($presenter);

src/Application/UI/Presenter.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,11 +1209,13 @@ private function initGlobalParameters()
12091209
$selfParams = [];
12101210

12111211
$params = $this->request->getParameters();
1212-
if ($this->isAjax()) {
1213-
$params += $this->request->getPost();
1214-
}
1215-
if (($tmp = $this->request->getPost(self::SIGNAL_KEY)) !== NULL) {
1212+
if (($tmp = $this->request->getPost('_' . self::SIGNAL_KEY . '_')) !== NULL) {
12161213
$params[self::SIGNAL_KEY] = $tmp;
1214+
} elseif ($this->isAjax()) {
1215+
$params += $this->request->getPost();
1216+
if (($tmp = $this->request->getPost(self::SIGNAL_KEY)) !== NULL) {
1217+
$params[self::SIGNAL_KEY] = $tmp;
1218+
}
12171219
}
12181220

12191221
foreach ($params as $key => $value) {

tests/UI/Presenter.parameters.phpt

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,33 @@ test(function () {
6666
$presenter->run(new Application\Request('Foo', 'POST', [], [
6767
'do' => 'foo'
6868
]));
69+
Assert::null($presenter->getSignal());
70+
});
71+
72+
test(function () {
73+
//_signal_ in POST
74+
$presenter = createPresenter();
75+
$presenter->run(new Application\Request('Foo', 'POST', [], [
76+
'_do_' => 'foo'
77+
]));
6978
Assert::same(['', 'foo'], $presenter->getSignal());
7079
});
7180

7281
test(function () {
73-
//signal in POST overwriting empty GET
82+
//signal in POST not overwriting GET
7483
$presenter = createPresenter();
7584
$presenter->run(new Application\Request('Foo', 'POST', ['do' => NULL], [
7685
'do' => 'foo'
7786
]));
87+
Assert::null($presenter->getSignal());
88+
});
89+
90+
test(function () {
91+
//_signal_ in POST overwriting GET
92+
$presenter = createPresenter();
93+
$presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [
94+
'_do_' => 'foo'
95+
]));
7896
Assert::same(['', 'foo'], $presenter->getSignal());
7997
});
8098

@@ -89,11 +107,21 @@ test(function () {
89107
});
90108

91109
test(function () {
92-
//AJAX: signal in POST overwriting empty GET
110+
//AJAX: signal in POST overwriting GET
93111
$presenter = createPresenter();
94112
$presenter->ajax = TRUE;
95-
$presenter->run(new Application\Request('Foo', 'POST', ['do' => NULL], [
113+
$presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [
96114
'do' => 'foo'
97115
]));
98116
Assert::same(['', 'foo'], $presenter->getSignal());
99117
});
118+
119+
test(function () {
120+
//AJAX: _signal_ in POST overwriting GET
121+
$presenter = createPresenter();
122+
$presenter->ajax = TRUE;
123+
$presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [
124+
'_do_' => 'foo'
125+
]));
126+
Assert::same(['', 'foo'], $presenter->getSignal());
127+
});

0 commit comments

Comments
 (0)