Skip to content

Commit

Permalink
Presenter: don't mix GET & POST params in AJAX when form is submitted [
Browse files Browse the repository at this point in the history
…Closes nette/forms#33][Closes nette/nette#1061] (#127)
  • Loading branch information
dg committed Apr 29, 2016
1 parent 9d71d64 commit c0931f4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Application/UI/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function attached($presenter)
$this->setAction(new Link($presenter, 'this', []));
$signal = new Nette\Forms\Controls\HiddenField($name . self::NAME_SEPARATOR . 'submit');
$signal->setOmitted()->setHtmlId(FALSE);
$this[Presenter::SIGNAL_KEY] = $signal;
$this['_' . Presenter::SIGNAL_KEY . '_'] = $signal;
}
}
parent::attached($presenter);
Expand Down
10 changes: 6 additions & 4 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1209,11 +1209,13 @@ private function initGlobalParameters()
$selfParams = [];

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

foreach ($params as $key => $value) {
Expand Down
34 changes: 31 additions & 3 deletions tests/UI/Presenter.parameters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,33 @@ test(function () {
$presenter->run(new Application\Request('Foo', 'POST', [], [
'do' => 'foo'
]));
Assert::null($presenter->getSignal());
});

test(function () {
//_signal_ in POST
$presenter = createPresenter();
$presenter->run(new Application\Request('Foo', 'POST', [], [
'_do_' => 'foo'
]));
Assert::same(['', 'foo'], $presenter->getSignal());
});

test(function () {
//signal in POST overwriting empty GET
//signal in POST not overwriting GET
$presenter = createPresenter();
$presenter->run(new Application\Request('Foo', 'POST', ['do' => NULL], [
'do' => 'foo'
]));
Assert::null($presenter->getSignal());
});

test(function () {
//_signal_ in POST overwriting GET
$presenter = createPresenter();
$presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [
'_do_' => 'foo'
]));
Assert::same(['', 'foo'], $presenter->getSignal());
});

Expand All @@ -89,11 +107,21 @@ test(function () {
});

test(function () {
//AJAX: signal in POST overwriting empty GET
//AJAX: signal in POST overwriting GET
$presenter = createPresenter();
$presenter->ajax = TRUE;
$presenter->run(new Application\Request('Foo', 'POST', ['do' => NULL], [
$presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [
'do' => 'foo'
]));
Assert::same(['', 'foo'], $presenter->getSignal());
});

test(function () {
//AJAX: _signal_ in POST overwriting GET
$presenter = createPresenter();
$presenter->ajax = TRUE;
$presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [
'_do_' => 'foo'
]));
Assert::same(['', 'foo'], $presenter->getSignal());
});

0 comments on commit c0931f4

Please sign in to comment.