Skip to content

Commit 6d792ea

Browse files
committed
BaseControl: supports setHtmlAttribute('name')
1 parent 97e6400 commit 6d792ea

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Forms/Controls/BaseControl.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected function getHttpData($type, string $htmlTail = null)
147147
*/
148148
public function getHtmlName(): string
149149
{
150-
return Nette\Forms\Helpers::generateHtmlName($this->lookupPath(Form::class));
150+
return $this->control->name ?? Nette\Forms\Helpers::generateHtmlName($this->lookupPath(Form::class));
151151
}
152152

153153

@@ -347,6 +347,9 @@ public function getHtmlId()
347347
public function setHtmlAttribute(string $name, $value = true)
348348
{
349349
$this->control->$name = $value;
350+
if ($name === 'name' && ($form = $this->getForm(false)) && !$this->isDisabled() && $form->isAnchored() && $form->isSubmitted()) {
351+
$this->loadHttpData();
352+
}
350353
return $this;
351354
}
352355

tests/Forms/Controls.BaseControl.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,17 @@ test(function () {
179179
Assert::match('<label for="frm-list2">list</label>', (string) $input->getLabel());
180180
Assert::same(['"list" field is required.'], $input->getErrors());
181181
});
182+
183+
184+
test(function () { // change HTML name
185+
$_POST = ['b' => '123', 'send' => ''];
186+
$form = new Form;
187+
$form->addSubmit('send', 'Send');
188+
$input = $form->addText('a');
189+
190+
Assert::same('', $input->getValue());
191+
$input->setHtmlAttribute('name', 'b');
192+
Assert::same('123', $input->getValue());
193+
194+
Assert::match('<input type="text" name="b" id="frm-a" value="123">', (string) $input->getControl());
195+
});

0 commit comments

Comments
 (0)