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
9 changes: 9 additions & 0 deletions src/FormElement/CheckboxElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,13 @@ public function renderUnwrapped()

return (new HiddenElement($this->getValueOfNameAttribute(), ['value' => $this->getUncheckedValue()])) . $html;
}

/**
* Determine if the checkbox is considered "checked".
* Returns true if the current value matches the checked value, otherwise false.
*/
public function hasValue(): bool
{
return $this->getValue() === $this->getCheckedValue();
}
}
16 changes: 16 additions & 0 deletions tests/FormElement/CheckboxElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
namespace ipl\Tests\Html;

use ipl\Html\FormElement\CheckboxElement;
use ipl\I18n\NoopTranslator;
use ipl\I18n\StaticTranslator;

class CheckboxElementTest extends TestCase
{
protected function setUp(): void
{
StaticTranslator::$instance = new NoopTranslator();
}

public function testRendersCheckValuesAsValueAttribute()
{
$checkbox = new CheckboxElement('test');
Expand Down Expand Up @@ -106,4 +113,13 @@ public function testSetCheckValues()
$checkbox
);
}

public function testCheckboxNotValidIfRequiredAndUnchecked()
{
$checkbox = new CheckboxElement('test');
$checkbox->setRequired();
$checkbox->setChecked(false);

$this->assertFalse($checkbox->isValid());
}
}
Loading