Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Inputs/RadioInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function getControl(): Html

$items = $this->getItems();
$container = $this->container;
// one attribute on the fieldset disables every radio inside it, the same
// way CheckboxListInput handles a wholly disabled control
$container->setAttribute('disabled', $this->isControlDisabled());

$c = 0;
$htmlId = $this->getHtmlId();
Expand Down
19 changes: 19 additions & 0 deletions tests/E2E/FormSubmissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,25 @@ function (): BootstrapForm {
$this->assertNull($form['country']->getValue());
}

public function testWhollyDisabledRadioListIsRenderedDisabledAndRejectsItsPost(): void
{
$form = $this->submit(
function (): BootstrapForm {
$form = new BootstrapForm();
$form->setAction('/');
$form->addRadioList('size', 'Size', ['s' => 'Small', 'l' => 'Large'])
->setDisabled(true);
$form->addSubmit('send');

return $form;
},
['size' => 's']
);

$this->assertNull($form['size']->getValue());
$this->assertStringContainsString('<fieldset disabled>', (string) $form['size']->getControl());
}

public function testCheckboxListCollectsEveryCheckedValue(): void
{
$form = $this->submit(
Expand Down
16 changes: 16 additions & 0 deletions tests/Inputs/RadioInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ public function testRadioInput(): void
$this->assertEquals('<fieldset><div class="custom-control custom-radio"><input class="custom-control-input" type="radio" value="1" name="txt" id="frm-txt0" data-nette-rules="[]"><label class="custom-control-label" for="frm-txt0">1</label></div><div class="custom-control custom-radio"><input class="custom-control-input" type="radio" value="2" name="txt" id="frm-txt1"><label class="custom-control-label" for="frm-txt1">2</label></div></fieldset>', (string) $input->getControl());
}

public function testDisabledRadioInput(): void
{
$form = new BootstrapForm();
$input = $form->addRadioList('txt', 'lbl', [1 => '1', 2 => '2']);
$input->setDisabled(true);
$this->assertEquals('<fieldset disabled><div class="custom-control custom-radio"><input class="custom-control-input" type="radio" value="1" name="txt" id="frm-txt0" data-nette-rules="[]"><label class="custom-control-label" for="frm-txt0">1</label></div><div class="custom-control custom-radio"><input class="custom-control-input" type="radio" value="2" name="txt" id="frm-txt1"><label class="custom-control-label" for="frm-txt1">2</label></div></fieldset>', (string) $input->getControl());
}

public function testRadioInputWithSingleDisabledItem(): void
{
$form = new BootstrapForm();
$input = $form->addRadioList('txt', 'lbl', [1 => '1', 2 => '2']);
$input->setDisabled([1]);
$this->assertEquals('<fieldset><div class="custom-control custom-radio"><input class="custom-control-input" type="radio" value="1" name="txt" disabled id="frm-txt0" data-nette-rules="[]"><label class="custom-control-label" for="frm-txt0">1</label></div><div class="custom-control custom-radio"><input class="custom-control-input" type="radio" value="2" name="txt" id="frm-txt1"><label class="custom-control-label" for="frm-txt1">2</label></div></fieldset>', (string) $input->getControl());
}

public function testRadioInputV5(): void
{
BootstrapForm::switchBootstrapVersion(BootstrapVersion::V5);
Expand Down
35 changes: 27 additions & 8 deletions tests/Traits/ChoiceInputTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,41 @@
class ChoiceInputTraitTest extends BaseTestCase
{

/**
* Known gap: RadioInput::getControl() only ever asks isValueDisabled() per
* item and never calls isControlDisabled(), so a radio list disabled as a
* whole still renders as interactive. CheckboxListInput and SelectInput both
* put the attribute on their element. Pinned here so that fixing RadioInput
* shows up as a failure of this test rather than going unnoticed.
*/
public function testDisablingTheWholeRadioListIsNotReflectedInTheHtml(): void
public function testWholeRadioListIsDisabledThroughItsFieldset(): void
{
$form = new BootstrapForm();
$radio = $form->addRadioList('a', 'b', ['x' => 'X', 'y' => 'Y']);
$radio->setDisabled(true);

$html = (string) $radio->getControl();

// one attribute on the fieldset disables every radio inside it
$this->assertStringStartsWith('<fieldset disabled>', $html);
$this->assertSame(1, substr_count($html, 'disabled'));
}

public function testReEnablingTheWholeRadioListDropsTheAttributeAgain(): void
{
$form = new BootstrapForm();
$radio = $form->addRadioList('a', 'b', ['x' => 'X', 'y' => 'Y']);
$radio->setDisabled(true);
$radio->setDisabled(false);

$this->assertStringNotContainsString('disabled', (string) $radio->getControl());
}

public function testDisabledRadioListKeepsItsFieldsetAttributeThroughValidationState(): void
{
$form = new BootstrapForm();
$radio = $form->addRadioList('a', 'b', ['x' => 'X', 'y' => 'Y']);
$radio->setDisabled(true);
$radio->addError('nope');

$html = (string) $radio->showValidation($radio->getControl());

$this->assertStringStartsWith('<fieldset disabled>', $html);
}

public function testDisablingRadioListClearsWhateverWasSelected(): void
{
$form = new BootstrapForm();
Expand Down
Loading