Skip to content

Commit 54858a9

Browse files
committed
added Form::initialize() for standalone forms
1 parent 0b7fe74 commit 54858a9

21 files changed

+61
-3
lines changed

src/Forms/Form.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class Form extends Container implements Nette\HtmlStringable
103103
/** @var bool */
104104
protected $crossOrigin = false;
105105

106+
/** @var Nette\Http\IRequest */
107+
private static $defaultHttpRequest;
108+
106109
/** @var mixed or null meaning: not detected yet */
107110
private $submittedBy;
108111

@@ -666,6 +669,32 @@ public function getToggles(): array
666669
/********************* backend ****************d*g**/
667670

668671

672+
/**
673+
* Initialize standalone forms.
674+
*/
675+
public static function initialize(bool $reinit = false): void
676+
{
677+
if ($reinit) {
678+
self::$defaultHttpRequest = null;
679+
return;
680+
} elseif (self::$defaultHttpRequest) {
681+
return;
682+
}
683+
684+
self::$defaultHttpRequest = (new Nette\Http\RequestFactory)->fromGlobals();
685+
686+
if (PHP_SAPI !== 'cli') {
687+
if (headers_sent($file, $line)) {
688+
throw new Nette\InvalidStateException(
689+
'Create a form or call Nette\Forms\Form::initialize() before the headers are sent to initialize CSRF protection.'
690+
. ($file ? " (output started at $file:$line)" : '') . '. '
691+
);
692+
}
693+
Nette\Http\Helpers::initCookie(self::$defaultHttpRequest, new Nette\Http\Response);
694+
}
695+
}
696+
697+
669698
/** @internal */
670699
public function setHttpRequest(Nette\Http\IRequest $request)
671700
{
@@ -676,9 +705,8 @@ public function setHttpRequest(Nette\Http\IRequest $request)
676705
private function getHttpRequest(): Nette\Http\IRequest
677706
{
678707
if (!$this->httpRequest) {
679-
$factory = new Nette\Http\RequestFactory;
680-
$this->httpRequest = $factory->createHttpRequest();
681-
Nette\Http\Helpers::initCookie($this->httpRequest, new Nette\Http\Response);
708+
self::initialize();
709+
$this->httpRequest = self::$defaultHttpRequest;
682710
}
683711
return $this->httpRequest;
684712
}

tests/Forms/Container.values.ArrayHash.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ $_POST = [
2424

2525
function createForm(): Form
2626
{
27+
Form::initialize(true);
28+
2729
$form = new Form;
2830
$form->addText('title');
2931

tests/Forms/Container.values.array.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ $_POST = [
2424

2525
function createForm(): Form
2626
{
27+
Form::initialize(true);
28+
2729
$form = new Form;
2830
$form->addText('title');
2931

tests/Forms/Container.values.mapping.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ $_POST = [
6464

6565
function createForm(): Form
6666
{
67+
Form::initialize(true);
68+
6769
$form = new Form;
6870
$form->addText('title');
6971

tests/Forms/Controls.BaseControl.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ use Tester\Assert;
1414
require __DIR__ . '/../bootstrap.php';
1515

1616

17+
before(function () {
18+
Form::initialize(true);
19+
});
20+
21+
1722
test('error handling', function () {
1823
$form = new Form;
1924
$input = $form->addText('text')

tests/Forms/Controls.Button.loadData.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ before(function () {
1717
$_SERVER['REQUEST_METHOD'] = 'POST';
1818
$_POST = $_FILES = [];
1919
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
20+
Form::initialize(true);
2021
});
2122

2223

tests/Forms/Controls.Checkbox.loadData.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ before(function () {
1717
$_SERVER['REQUEST_METHOD'] = 'POST';
1818
$_POST = $_FILES = [];
1919
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
20+
Form::initialize(true);
2021
});
2122

2223

tests/Forms/Controls.CheckboxList.loadData.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ before(function () {
1919
$_SERVER['REQUEST_METHOD'] = 'POST';
2020
$_POST = $_FILES = [];
2121
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
22+
Form::initialize(true);
2223
});
2324

2425

tests/Forms/Controls.ChoiceControl.loadData.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ before(function () {
2323
$_SERVER['REQUEST_METHOD'] = 'POST';
2424
$_POST = $_FILES = [];
2525
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
26+
Form::initialize(true);
2627
});
2728

2829

tests/Forms/Controls.HiddenField.loadData.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ before(function () {
1717
$_SERVER['REQUEST_METHOD'] = 'POST';
1818
$_POST = $_FILES = [];
1919
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
20+
Form::initialize(true);
2021
});
2122

2223

0 commit comments

Comments
 (0)