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) + )); } } 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');