From 14381fd3f3cd8c75f468c7cecd65d054e7a8664a Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Tue, 11 Apr 2023 12:21:10 +0200 Subject: [PATCH 1/2] CompatForm: Fix wrong label being set for duplicate submit button --- src/Compat/CompatForm.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Compat/CompatForm.php b/src/Compat/CompatForm.php index e918b228..1a41243a 100644 --- a/src/Compat/CompatForm.php +++ b/src/Compat/CompatForm.php @@ -2,8 +2,10 @@ namespace ipl\Web\Compat; +use http\Exception\InvalidArgumentException; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\Form; +use ipl\Html\FormElement\SubmitButtonElement; use ipl\Html\FormElement\SubmitElement; use ipl\Html\HtmlDocument; use ipl\Html\HtmlString; @@ -53,14 +55,31 @@ public function hasDefaultElementDecorator() * * @param FormSubmitElement $originalSubmitButton * - * @return SubmitElement + * @return FormSubmitElement */ - public function duplicateSubmitButton(FormSubmitElement $originalSubmitButton): SubmitElement + public function duplicateSubmitButton(FormSubmitElement $originalSubmitButton): FormSubmitElement { $attributes = (clone $originalSubmitButton->getAttributes()) ->set('class', 'primary-submit-btn-duplicate'); $attributes->remove('id'); + // Remove to avoid `type="submit submit"` in SubmitButtonElement + $attributes->remove('type'); - return new SubmitElement($originalSubmitButton->getName(), $attributes); + if ($originalSubmitButton instanceof SubmitElement) { + $newSubmitButton = new SubmitElement($originalSubmitButton->getName(), $attributes); + $newSubmitButton->setLabel($originalSubmitButton->getButtonLabel()); + + return $newSubmitButton; + } elseif ($originalSubmitButton instanceof SubmitButtonElement) { + $newSubmitButton = new SubmitButtonElement($originalSubmitButton->getName(), $attributes); + $newSubmitButton->setSubmitValue($originalSubmitButton->getSubmitValue()); + + return $newSubmitButton; + } + + throw new InvalidArgumentException(sprintf( + 'Cannot duplicate submit button of type "%s"', + get_class($originalSubmitButton) + )); } } From 78c15b2ce76b3ac525e98a330f79bf931d471346 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Wed, 12 Apr 2023 11:41:59 +0200 Subject: [PATCH 2/2] CompatFormTest: Add `SubmitElement` and `SubmitButtonElement` test cases for duplicate button --- tests/Compat/CompatFormTest.php | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/Compat/CompatFormTest.php b/tests/Compat/CompatFormTest.php index ef81ee76..89b58fd9 100644 --- a/tests/Compat/CompatFormTest.php +++ b/tests/Compat/CompatFormTest.php @@ -36,6 +36,62 @@ public function testDuplicateSubmitButtonApplied(): void $this->assertHtml($expected, $this->form); } + public function testSubmitElementDuplication(): void + { + $this->form->addElement('submit', 'submit', [ + 'label' => 'Submit label', + 'class' => 'btn-primary' + ]); + $this->form->addElement('submit', 'delete', [ + 'label' => 'Delete label', + 'class' => 'btn-danger' + ]); + $this->form->setSubmitButton($this->form->getElement('submit')); + + $expected = <<<'HTML' +
+ +
+ +
+
+ +
+
+HTML; + + $this->assertHtml($expected, $this->form); + } + + + public function testSubmitButtonElementDuplication(): void + { + $this->form->addElement('submitButton', 'submit', [ + 'label' => 'Submit label', + 'class' => 'btn-primary', + 'value' => 'submit_value' + ]); + $this->form->addElement('submitButton', 'delete', [ + 'label' => 'Delete label', + 'class' => 'btn-danger' + ]); + $this->form->setSubmitButton($this->form->getElement('submit')); + + $expected = <<<'HTML' +
+ + +
+ +
+
+HTML; + + $this->assertHtml($expected, $this->form); + } + public function testDuplicateSubmitButtonOmitted(): void { $this->form->addElement('submit', 'submitCreate');