From c78fed047b4dab807fe20678451b172062e741ba Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 11 Feb 2024 17:45:48 +0100 Subject: [PATCH] BaseControl: added 'form' attribute --- src/Forms/Controls/BaseControl.php | 1 + src/Forms/Controls/Button.php | 4 +- src/Forms/Controls/HiddenField.php | 1 + tests/Forms/Forms.form.phpt | 31 +++++++++++ tests/Forms/expected/Forms.form.expect | 72 ++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 tests/Forms/Forms.form.phpt create mode 100644 tests/Forms/expected/Forms.form.expect diff --git a/src/Forms/Controls/BaseControl.php b/src/Forms/Controls/BaseControl.php index bac909dff..a9cbe41f4 100644 --- a/src/Forms/Controls/BaseControl.php +++ b/src/Forms/Controls/BaseControl.php @@ -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, ]); } diff --git a/src/Forms/Controls/Button.php b/src/Forms/Controls/Button.php index 021bbcc7b..dd4c56472 100644 --- a/src/Forms/Controls/Button.php +++ b/src/Forms/Controls/Button.php @@ -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); diff --git a/src/Forms/Controls/HiddenField.php b/src/Forms/Controls/HiddenField.php index 2f5dd8120..bcbb18e03 100644 --- a/src/Forms/Controls/HiddenField.php +++ b/src/Forms/Controls/HiddenField.php @@ -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, ]); } diff --git a/tests/Forms/Forms.form.phpt b/tests/Forms/Forms.form.phpt new file mode 100644 index 000000000..83e51b3e6 --- /dev/null +++ b/tests/Forms/Forms.form.phpt @@ -0,0 +1,31 @@ +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)); diff --git a/tests/Forms/expected/Forms.form.expect b/tests/Forms/expected/Forms.form.expect new file mode 100644 index 000000000..7c407a4bb --- /dev/null +++ b/tests/Forms/expected/Forms.form.expect @@ -0,0 +1,72 @@ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
\ No newline at end of file