Skip to content

Commit

Permalink
added Form::initialize() for standalone forms
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 11, 2021
1 parent 0a977a1 commit 17bfaf9
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Forms/Form.php
Expand Up @@ -103,6 +103,9 @@ class Form extends Container implements Nette\HtmlStringable
/** @var bool */
protected $crossOrigin = false;

/** @var Nette\Http\IRequest */
private static $defaultHttpRequest;

/** @var mixed or null meaning: not detected yet */
private $submittedBy;

Expand Down Expand Up @@ -666,6 +669,21 @@ public function getToggles(): array
/********************* backend ****************d*g**/


/**
* Initialize standalone forms.
*/
public static function initialize(bool $reinit = false): void
{
if ($reinit) {
self::$defaultHttpRequest = null;
} elseif (!self::$defaultHttpRequest) {
$factory = new Nette\Http\RequestFactory;
self::$defaultHttpRequest = $factory->fromGlobals();
Nette\Http\Helpers::initCookie(self::$defaultHttpRequest, new Nette\Http\Response);
}
}


/** @internal */
public function setHttpRequest(Nette\Http\IRequest $request)
{
Expand All @@ -676,9 +694,8 @@ public function setHttpRequest(Nette\Http\IRequest $request)
private function getHttpRequest(): Nette\Http\IRequest
{
if (!$this->httpRequest) {
$factory = new Nette\Http\RequestFactory;
$this->httpRequest = $factory->createHttpRequest();
Nette\Http\Helpers::initCookie($this->httpRequest, new Nette\Http\Response);
self::initialize();
$this->httpRequest = self::$defaultHttpRequest;
}
return $this->httpRequest;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Forms/Container.values.ArrayHash.phpt
Expand Up @@ -24,6 +24,8 @@ $_POST = [

function createForm(): Form
{
Form::initialize(true);

$form = new Form;
$form->addText('title');

Expand Down
2 changes: 2 additions & 0 deletions tests/Forms/Container.values.array.phpt
Expand Up @@ -24,6 +24,8 @@ $_POST = [

function createForm(): Form
{
Form::initialize(true);

$form = new Form;
$form->addText('title');

Expand Down
2 changes: 2 additions & 0 deletions tests/Forms/Container.values.mapping.phpt
Expand Up @@ -64,6 +64,8 @@ $_POST = [

function createForm(): Form
{
Form::initialize(true);

$form = new Form;
$form->addText('title');

Expand Down
5 changes: 5 additions & 0 deletions tests/Forms/Controls.BaseControl.phpt
Expand Up @@ -14,6 +14,11 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


before(function () {
Form::initialize(true);
});


test('error handling', function () {
$form = new Form;
$input = $form->addText('text')
Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.Button.loadData.phpt
Expand Up @@ -17,6 +17,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.Checkbox.loadData.phpt
Expand Up @@ -17,6 +17,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.CheckboxList.loadData.phpt
Expand Up @@ -19,6 +19,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.ChoiceControl.loadData.phpt
Expand Up @@ -23,6 +23,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.HiddenField.loadData.phpt
Expand Up @@ -17,6 +17,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.ImageButton.loadData.phpt
Expand Up @@ -17,6 +17,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.MultiChoiceControl.loadData.phpt
Expand Up @@ -24,6 +24,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.MultiSelectBox.loadData.phpt
Expand Up @@ -19,6 +19,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.RadioList.loadData.phpt
Expand Up @@ -18,6 +18,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.SelectBox.isOk.phpt
Expand Up @@ -44,6 +44,7 @@ Assert::false($select->isOk());
// error message is processed via Rules
$_SERVER['REQUEST_METHOD'] = 'POST';
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
Validator::$messages[Nette\Forms\Controls\SelectBox::VALID] = 'SelectBox "%label" must be filled.';
$form = new Form;
$form->addSelect('foo', 'Foo', ['bar' => 'Bar']);
Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.SelectBox.loadData.phpt
Expand Up @@ -18,6 +18,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.TextBase.loadData.phpt
Expand Up @@ -17,6 +17,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Forms.crossOrigin.phpt
Expand Up @@ -16,6 +16,7 @@ require __DIR__ . '/../bootstrap.php';
before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_GET = $_POST = $_FILES = $_COOKIE = [];
Form::initialize(true);
});


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Forms.getHttpData.get.phpt
Expand Up @@ -16,6 +16,7 @@ require __DIR__ . '/../bootstrap.php';
before(function () {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET = $_POST = $_FILES = [];
Form::initialize(true);
});


Expand Down
4 changes: 4 additions & 0 deletions tests/Forms/Forms.getHttpData.post.phpt
Expand Up @@ -18,6 +18,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
$_GET = $_POST = $_FILES = [];
Form::initialize(true);
});


Expand Down Expand Up @@ -76,7 +77,10 @@ test('', function () {
$input = $form->addSubmit('send', 'Send');
Assert::false($input->isSubmittedBy());
Assert::false(Validator::validateSubmitted($input));
});


test('', function () {
$_POST = ['send' => ''];
$form = new Form;
$input = $form->addSubmit('send', 'Send');
Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Forms.submittedBy.phpt
Expand Up @@ -17,6 +17,7 @@ before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
$_GET = $_POST = $_FILES = [];
Form::initialize(true);
});


Expand Down

0 comments on commit 17bfaf9

Please sign in to comment.