Skip to content

Commit 00a923b

Browse files
ryderczdg
authored andcommitted
DefaultFormRenderer: strict type fix (#220)
fixes "TypeError: preg_split() expects parameter 2 to be string, null given" for GET form if action does not contain query parameters
1 parent f0aa853 commit 00a923b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Forms/Rendering/DefaultFormRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function renderBegin(): string
167167

168168
if ($this->form->isMethod('get')) {
169169
$el = clone $this->form->getElementPrototype();
170-
$query = parse_url($el->action, PHP_URL_QUERY);
170+
$query = parse_url($el->action, PHP_URL_QUERY) ?: '';
171171
$el->action = str_replace("?$query", '', $el->action);
172172
$s = '';
173173
foreach (preg_split('#[;&]#', $query, -1, PREG_SPLIT_NO_EMPTY) as $param) {

tests/Forms/Forms.renderer.6.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Forms default rendering GET form.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Tester\Assert;
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
$form = new Nette\Forms\Form;
15+
$form->setMethod('GET');
16+
$form->setAction('link');
17+
$form->addCheckboxList('list')
18+
->setItems(['First', 'Second']);
19+
$form->addHidden('userid');
20+
$form->addSubmit('submit', 'Send');
21+
22+
$form->fireEvents();
23+
24+
Assert::match('<form action="link" method="get">
25+
26+
<table>
27+
<tr>
28+
<th><label></label></th>
29+
30+
<td><label><input type="checkbox" name="list[]" value="0">First</label><br><label><input type="checkbox" name="list[]" value="1">Second</label></td>
31+
</tr>
32+
33+
<tr>
34+
<th></th>
35+
36+
<td><input type="submit" name="_submit" value="Send" class="button"></td>
37+
</tr>
38+
</table>
39+
40+
<input type="hidden" name="userid" value=""><!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
41+
</form>', $form->__toString(true));
42+
43+
Assert::same('link', $form->getAction());

0 commit comments

Comments
 (0)