Skip to content

Commit ad54573

Browse files
h4kunadg
authored andcommitted
DefaultFormRenderer, Latte: fix input name defined like array (#182)
1 parent 1da7145 commit ad54573

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/Bridges/FormsLatte/Runtime.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
5252
foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) {
5353
$parts = explode('=', $param, 2);
5454
$name = urldecode($parts[0]);
55-
if (!isset($form[$name])) {
55+
$prefix = explode('[', $name, 2)[0];
56+
if (!isset($form[$prefix])) {
5657
$s .= Html::el('input', ['type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])]);
5758
}
5859
}

src/Forms/Rendering/DefaultFormRenderer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public function renderBegin(): string
172172
foreach (preg_split('#[;&]#', $query, -1, PREG_SPLIT_NO_EMPTY) as $param) {
173173
$parts = explode('=', $param, 2);
174174
$name = urldecode($parts[0]);
175-
if (!isset($this->form[$name])) {
175+
$prefix = explode('[', $name, 2)[0];
176+
if (!isset($this->form[$prefix])) {
176177
$s .= Html::el('input', ['type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])]);
177178
}
178179
}

tests/Forms.Latte/Runtime.get.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ require __DIR__ . '/../bootstrap.php';
1515

1616
$form = new Form;
1717
$form->setMethod('GET');
18-
$form->action = 'http://example.com/?do=foo-submit#toc';
18+
$form->addText('arg1');
19+
$form->addText('arg2');
20+
$form->setAction('http://example.com/?do=foo-submit&arg0=1&arg1=1&arg2[x]=1#toc');
1921

2022
Assert::same(
2123
'<form action="http://example.com/#toc" method="get">',
2224
Runtime::renderFormBegin($form, [])
2325
);
2426

2527
Assert::match(
26-
'<input type="hidden" name="do" value="foo-submit"><!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
27-
</form>',
28+
'<input type="hidden" name="do" value="foo-submit"><input type="hidden" name="arg0" value="1"></form>',
2829
Runtime::renderFormEnd($form)
2930
);

tests/Forms/Forms.renderer.4.phpt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ require __DIR__ . '/../bootstrap.php';
1313

1414
$form = new Nette\Forms\Form;
1515
$form->setMethod('GET');
16-
$form->setAction('link?a=b&c[]=d');
16+
$form->setAction('link?a=b&c[]=d&list[0]=1');
17+
$form->addCheckboxList('list')
18+
->setItems(['First', 'Second']);
1719
$form->addHidden('userid');
1820
$form->addSubmit('submit', 'Send');
1921

@@ -23,6 +25,12 @@ Assert::match('<form action="link" method="get">
2325
<input type="hidden" name="a" value="b"><input type="hidden" name="c[]" value="d">
2426
2527
<table>
28+
<tr>
29+
<th><label></label></th>
30+
31+
<td><label><input type="checkbox" name="list[]" value="0">First</label><br><label><input type="checkbox" name="list[]" value="1">Second</label></td>
32+
</tr>
33+
2634
<tr>
2735
<th></th>
2836
@@ -33,4 +41,4 @@ Assert::match('<form action="link" method="get">
3341
<input type="hidden" name="userid" value=""><!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
3442
</form>', $form->__toString(true));
3543

36-
Assert::same('link?a=b&c[]=d', $form->getAction());
44+
Assert::same('link?a=b&c[]=d&list[0]=1', $form->getAction());

0 commit comments

Comments
 (0)