Skip to content

Commit

Permalink
Merge pull request #136 from Icinga/fix-duplicate-button-submitting
Browse files Browse the repository at this point in the history
Fix duplicate submit button not triggering `getPressedSubmitElement()`
  • Loading branch information
nilmerg authored Apr 12, 2023
2 parents 7c7ca7e + 78c15b2 commit 82dcd7d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/Compat/CompatForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
));
}
}
56 changes: 56 additions & 0 deletions tests/Compat/CompatFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<form class="icinga-form icinga-controls" method="POST">
<input class="primary-submit-btn-duplicate" name="submit" type="submit" value="Submit label"/>
<div class="control-group form-controls">
<input class="btn-primary btn-primary" name="submit" type="submit" value="Submit label"/>
</div>
<div class="control-group form-controls">
<input class="btn-danger btn-primary" name="delete" type="submit" value="Delete label"/>
</div>
</form>
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'
<form class="icinga-form icinga-controls" method="POST">
<button class="primary-submit-btn-duplicate" name="submit" type="submit" value="submit_value" />
<div class="control-group form-controls">
<button class="btn-primary btn-primary" name="submit" type="submit" value="submit_value">Submit label</button>
</div>
<div class="control-group form-controls">
<button class="btn-danger btn-primary" name="delete" type="submit" value="y">Delete label</button>
</div>
</form>
HTML;

$this->assertHtml($expected, $this->form);
}

public function testDuplicateSubmitButtonOmitted(): void
{
$this->form->addElement('submit', 'submitCreate');
Expand Down

0 comments on commit 82dcd7d

Please sign in to comment.