Skip to content

Commit

Permalink
required flag on checkbox decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Apr 25, 2019
1 parent 9463e00 commit e79e3ed
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
56 changes: 43 additions & 13 deletions src/Decorators/CheckboxDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@

class CheckboxDecorator extends AbstractDataHandlerDecorator
{
protected $_required = false;

/**
* @return bool
*/
public function isRequired(): bool
{
return $this->_required;
}

/**
* @param bool $required
*
* @return $this
*/
public function setRequired(bool $required)
{
$this->_required = $required;
return $this;
}

protected function _initInputElement(): HtmlTag
{
return Div::create();
Expand All @@ -27,27 +48,29 @@ protected function _configureInputElement(HtmlElement $input)
$options = [];
foreach($this->_handler->getOptions() as $value => $label)
{
$options[] = $this->_getCheckbox(
$option = $this->_getCheckbox(
$name . Strings::pattern('-XXX-000'),
$name . '[]',
$value,
$label,
$currentValue
);
$options[] = $this->_getContainer($option, $label);
}
$input->setContent($options);
}
else
{
$input->setContent(
$this->_getCheckbox(
$name . Strings::pattern('-XXX-000'),
$name,
'true',
$this->_handler->getLabel(),
$currentValue ? 'true' : 'false'
)
$checkbox = $this->_getCheckbox(
$name . Strings::pattern('-XXX-000'),
$name,
'true',
$currentValue ? 'true' : 'false'
);
if($this->_required)
{
$checkbox->setAttribute('required', true);
}
$input->setContent($this->_getContainer($checkbox, $this->_handler->getLabel()));
}
}
}
Expand All @@ -62,7 +85,7 @@ protected function _formatElements(HtmlTag $input, ?HtmlTag $label, ?HtmlTag $er
return $elements;
}

private function _getCheckbox($id, $name, $value, $text, $currentValue)
private function _getCheckbox($id, $name, $value, $currentValue)
{
if(!is_array($currentValue))
{
Expand All @@ -77,7 +100,14 @@ private function _getCheckbox($id, $name, $value, $text, $currentValue)
{
$checkbox->setAttribute('checked', true);
}
return Div::create($checkbox, Label::create($text)->setAttribute('for', $id))
->addClass('p-form--checkbox');
return $checkbox;
}

private function _getContainer(Input $checkbox, $label)
{
return Div::create(
$checkbox,
Label::create($label)->setAttribute('for', $checkbox->getId())
)->addClass('p-form--checkbox');
}
}
15 changes: 13 additions & 2 deletions tests/DataHandlers/BooleanHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,42 @@
namespace Packaged\Tests\Form\DataHandlers;

use Packaged\Form\DataHandlers\BooleanDataHandler;
use Packaged\Form\Decorators\CheckboxDecorator;
use PHPUnit\Framework\TestCase;

class BooleanHandlerTest extends TestCase
{
public function testCheckbox()
{
$h = new BooleanDataHandler();
/** @var CheckboxDecorator $decorator */
$decorator = $h->getDecorator();
$h->setName('mychoice');
$h->setValue('sgfsafhasdg');
$this->assertFalse($h->isValid());
$this->assertFalse($decorator->isRequired());

$h->setValueFormatted('false');
$h->setLabel('Do You Agree?');
$this->assertTrue($h->isValid());

$this->assertRegExp(
'~<div class="p-form-field"><div class="p-form--input"><div><div class="p-form--checkbox"><input type="checkbox" id="(mychoice-...-...)" name="mychoice" value="true" /><label for="\1">Do You Agree\?</label></div></div></div></div>~',
$h->getDecorator()->render()
$decorator->render()
);

$h->setValueFormatted('yes');
$this->assertTrue($h->isValid());
$this->assertRegExp(
'~<div class="p-form-field"><div class="p-form--input"><div><div class="p-form--checkbox"><input type="checkbox" id="(mychoice-...-...)" name="mychoice" value="true" checked /><label for="\1">Do You Agree\?</label></div></div></div></div>~',
$h->getDecorator()->render()
$decorator->render()
);

$decorator->setRequired(true);
$this->assertTrue($decorator->isRequired());
$this->assertRegExp(
'~<div class="p-form-field"><div class="p-form--input"><div><div class="p-form--checkbox"><input type="checkbox" id="(mychoice-...-...)" name="mychoice" value="true" checked required /><label for="\1">Do You Agree\?</label></div></div></div></div>~',
$decorator->render()
);
}
}

0 comments on commit e79e3ed

Please sign in to comment.