-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from Icinga/PrefixSubmitButtonTrait
CompatForm: Insert duplicate submit button when more than one is present
- Loading branch information
Showing
4 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
Automatically set CSS class for duplicated submit buttons | ||
used for implicit form submission that should be invisible | ||
and not take up any space. `display: none` is not an option, | ||
because at least Safari will then ignore the element completely | ||
when submitting a form. | ||
*/ | ||
.primary-submit-btn-duplicate { | ||
border: 0; | ||
height: 0; | ||
margin: 0; | ||
padding: 0; | ||
visibility: hidden; | ||
width: 0; | ||
position: absolute; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace ipl\Tests\Web\Compat; | ||
|
||
use ipl\Html\FormElement\SubmitElement; | ||
use ipl\Tests\Html\TestCase; | ||
use ipl\Web\Compat\CompatForm; | ||
|
||
class CompatFormTest extends TestCase | ||
{ | ||
/** @var CompatForm */ | ||
private $form; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->form = new CompatForm(); | ||
} | ||
|
||
public function testDuplicateSubmitButtonApplied(): void | ||
{ | ||
$this->form->addElement('submit', 'submitCreate'); | ||
$this->form->addElement('submit', 'submitDelete'); | ||
|
||
$expected = <<<'HTML' | ||
<form class="icinga-form icinga-controls" method="POST"> | ||
<input class="primary-submit-btn-duplicate" name="submitCreate" type="submit" value="submitCreate"/> | ||
<div class="control-group form-controls"> | ||
<input class="btn-primary" name="submitCreate" type="submit" value="submitCreate"/> | ||
</div> | ||
<div class="control-group form-controls"> | ||
<input class="btn-primary" name="submitDelete" type="submit" value="submitDelete"/> | ||
</div> | ||
</form> | ||
HTML; | ||
|
||
$this->assertHtml($expected, $this->form); | ||
} | ||
|
||
public function testDuplicateSubmitButtonOmitted(): void | ||
{ | ||
$this->form->addElement('submit', 'submitCreate'); | ||
|
||
$expected = <<<'HTML' | ||
<form class="icinga-form icinga-controls" method="POST"> | ||
<div class="control-group form-controls"> | ||
<input class="btn-primary" name="submitCreate" type="submit" value="submitCreate"/> | ||
</div> | ||
</form> | ||
HTML; | ||
|
||
$this->assertHtml($expected, $this->form); | ||
} | ||
|
||
public function testDuplicateSubmitButtonAddedOnlyOnce(): void | ||
{ | ||
$this->form->addElement('submit', 'submitCreate', ['id' => 'submit_id']); | ||
$this->form->addElement('submit', 'submitDelete'); | ||
|
||
$expected = <<<'HTML' | ||
<form class="icinga-form icinga-controls" method="POST"> | ||
<input class="primary-submit-btn-duplicate" name="submitCreate" type="submit" value="submitCreate"/> | ||
<div class="control-group form-controls"> | ||
<input id="submit_id" class="btn-primary" name="submitCreate" type="submit" value="submitCreate"/> | ||
</div> | ||
<div class="control-group form-controls"> | ||
<input class="btn-primary" name="submitDelete" type="submit" value="submitDelete"/> | ||
</div> | ||
</form> | ||
HTML; | ||
|
||
// Call render twice to ensure that the submit button is only prepended once. | ||
$this->form->render(); | ||
$this->assertHtml($expected, $this->form); | ||
} | ||
|
||
public function testDuplicateSubmitButtonRespectsOriginalAttributes(): void | ||
{ | ||
$submitButton = new SubmitElement('test_submit', [ | ||
'class' => 'autosubmit', | ||
'formnovalidate' => true | ||
]); | ||
|
||
$prefixButton = $this->form->duplicateSubmitButton($submitButton); | ||
|
||
// Name should stay the same | ||
$this->assertSame($submitButton->getName(), 'test_submit'); | ||
$this->assertSame($prefixButton->getName(), 'test_submit'); | ||
|
||
// Added attributes should stay the same | ||
$this->assertSame($submitButton->getAttributes()->get('formnovalidate')->getValue(), true); | ||
$this->assertSame($prefixButton->getAttributes()->get('formnovalidate')->getValue(), true); | ||
|
||
// Class attribute should change to `primary-submit-btn-duplicate` | ||
$this->assertSame($submitButton->getAttributes()->get('class')->getValue(), 'autosubmit'); | ||
$this->assertSame($prefixButton->getAttributes()->get('class')->getValue(), 'primary-submit-btn-duplicate'); | ||
} | ||
} |