Skip to content

Commit

Permalink
BaseControl: added 'form' attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 11, 2024
1 parent a0e7f55 commit c78fed0
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public function getControl()
'required' => $this->isRequired(),
'disabled' => $this->isDisabled(),
'data-nette-rules' => Nette\Forms\Helpers::exportRules($this->rules) ?: null,
'form' => $el->form ?? $this->getForm()->getElementPrototype()->id,
]);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Forms/Controls/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ public function getControl(string|Stringable|null $caption = null): Html
{
$this->setOption('rendered', true);
$caption = $this->translate($caption ?? $this->getCaption());
$el = (clone $this->control)->addAttributes([
$el = clone $this->control;
$el->addAttributes([
'name' => $this->getHtmlName(),
'disabled' => $this->isDisabled(),
'form' => $el->form ?? $this->getForm()->getElementPrototype()->id,
]);
if ($caption instanceof Html || ($caption !== null && $el->getName() === 'button')) {
$el->setName('button')->setText($caption);
Expand Down
1 change: 1 addition & 0 deletions src/Forms/Controls/HiddenField.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function getControl(): Html
'name' => $this->getHtmlName(),
'disabled' => $this->isDisabled(),
'value' => (string) $this->value,
'form' => $el->form ?? $this->getForm()->getElementPrototype()->id,
]);
}

Expand Down
31 changes: 31 additions & 0 deletions tests/Forms/Forms.form.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use Nette\Forms\Form;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


$form = new Form;
$form->getElementPrototype()->id = 'frmid';

$form->addText('a');
$form->addTextArea('b');
$form->addDate('c');
$form->addUpload('d');
$form->addHidden('e');
$form->addCheckbox('f');
$form->addRadioList('g', null, ['item']);
$form->addCheckboxList('h', null, ['item']);
$form->addSelect('i');
$form->addMultiSelect('j');
$form->addColor('k');
$form->addSubmit('l');
$form->addButton('m');
$form->addImageButton('n');
$form->addHidden('none')->setHtmlAttribute('form', false);
$form->addHidden('diff')->setHtmlAttribute('form', 'different');

Assert::matchFile(__DIR__ . '/expected/Forms.form.expect', $form->__toString(true));
72 changes: 72 additions & 0 deletions tests/Forms/expected/Forms.form.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<form action="" method="post" id="frmid" enctype="multipart/form-data">

<table>
<tr>
<th><label for="frm-a"></label></th>

<td><input type="text" name="a" id="frm-a" form="frmid" class="text"></td>
</tr>

<tr>
<th><label for="frm-b"></label></th>

<td><textarea name="b" id="frm-b" form="frmid"></textarea></td>
</tr>

<tr>
<th><label for="frm-c"></label></th>

<td><input type="date" name="c" id="frm-c" form="frmid"></td>
</tr>

<tr>
<th><label for="frm-d"></label></th>

<td><input type="file" name="d" id="frm-d" data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to %d% bytes.","arg":%d%}]' form="frmid" class="text"></td>
</tr>

<tr>
<th></th>

<td><label for="frm-f"><input type="checkbox" name="f" id="frm-f" form="frmid"></label></td>
</tr>

<tr>
<th><label></label></th>

<td><label><input type="radio" name="g" form="frmid" value="0">item</label></td>
</tr>

<tr>
<th><label></label></th>

<td><label><input type="checkbox" name="h[]" form="frmid" value="0">item</label></td>
</tr>

<tr>
<th><label for="frm-i"></label></th>

<td><select name="i" id="frm-i" form="frmid"></select></td>
</tr>

<tr>
<th><label for="frm-j"></label></th>

<td><select name="j[]" id="frm-j" form="frmid" multiple></select></td>
</tr>

<tr>
<th><label for="frm-k"></label></th>

<td><input type="color" name="k" id="frm-k" form="frmid" value="#000000"></td>
</tr>

<tr>
<th></th>

<td><input type="submit" name="l" form="frmid" class="button"> <input type="button" name="m" form="frmid" class="button"> <input type="image" name="n[]" form="frmid" class="imagebutton"></td>
</tr>
</table>

<input type="hidden" name="e" value="" form="frmid"><input type="hidden" name="none" value=""><input type="hidden" name="diff" form="different" value="">
</form>

0 comments on commit c78fed0

Please sign in to comment.