diff --git a/demo/index.php b/demo/index.php index b21cfd2..1cc0602 100644 --- a/demo/index.php +++ b/demo/index.php @@ -71,6 +71,9 @@ class DemoForm extends Form /** @var \Packaged\Form\DataHandlers\MultiLineTextDataHandler */ public $comments; + /** @var EnumDataHandler */ + public $opions; + protected function _initDataHandlers() { $this->name = TextDataHandler::i()->addValidator(new StringValidator(2, 20)); @@ -104,12 +107,15 @@ protected function _initDataHandlers() ->setGuidance("Required") ->addValidator(new RequiredValidator()); //$this->setHandlerDecorator(new InputOnlyDataHandlerDecorator(), 'agree'); + + $this->opions = EnumDataHandler::i(); } protected function _configureDataHandlers() { parent::_configureDataHandlers(); $this->confirmPassword->addValidator(new ConfirmationValidator($this->password->getName())); + $this->opions->setOptions(['test1' => 'Test 1', 'test2' => 'Test 2', 'test3' => 'Test 3']); } } diff --git a/php/src/DataHandlers/EnumDataHandler.php b/php/src/DataHandlers/EnumDataHandler.php index ae9b39f..9fac86d 100644 --- a/php/src/DataHandlers/EnumDataHandler.php +++ b/php/src/DataHandlers/EnumDataHandler.php @@ -8,13 +8,15 @@ use Packaged\Glimpse\Tags\Form\Label; use Packaged\Glimpse\Tags\Form\Option; use Packaged\Glimpse\Tags\Form\Select; +use Packaged\Helpers\Arrays; use Packaged\Helpers\Strings; +use Packaged\Helpers\ValueAs; use Packaged\Ui\Html\HtmlElement; class EnumDataHandler extends AbstractDataHandler { const INPUT_STYLE_COMBINED = 'single'; - const INPUT_STYLE_SPLIT = 'split'; + const INPUT_STYLE_SPLIT = 'split'; protected $_options = []; protected $_inputStyle = self::INPUT_STYLE_COMBINED; @@ -91,33 +93,52 @@ protected function _generateSplitInput($key, $value, $selected = false): HtmlEle protected function _isSelectedOption($option): bool { - return $option === $this->getValue(); + return ValueAs::string($option) === $this->getValue(); } protected function _generateInput(): HtmlElement { + $isAssocArray = Arrays::isAssoc($this->getOptions()); + if($this->_inputStyle === self::INPUT_STYLE_SPLIT) { $ele = new MultiInputContainer(); - foreach($this->getOptions() as $optK => $optV) + if($isAssocArray) { - $ele->addInput($this->_generateSplitInput($optK, $optV, $this->_isSelectedOption($optK))); + foreach($this->getOptions() as $optK => $optV) + { + $ele->addInput($this->_generateSplitInput($optK, $optV, $this->_isSelectedOption($optK))); + } } + else + { + foreach($this->getOptions() as $optV) + { + $ele->addInput($this->_generateSplitInput($optV, $optV, $this->_isSelectedOption($optV))); + } + } + return $ele; } - $options = []; - foreach($this->getOptions() as $optK => $optV) + $options = Option::collection($this->getOptions()); + + /** @var Option $option */ + foreach($options as $option) { - $options[] = Option::create($optV)->addAttributes( - array_filter( - [ - 'value' => $optK, - 'selected' => $this->_isSelectedOption($optK), - ] - ) - ); + if($isAssocArray) + { + if($this->_isSelectedOption($option->getAttribute('value'))) + { + $option->setAttribute('selected', true); + } + } + else if($this->_isSelectedOption($option->getContent(false))) + { + $option->setAttribute('selected', true); + } } + $ele = Select::create($options); $ele->addAttributes( [ diff --git a/php/tests/DataHandlers/EnumDataHandlerTest.php b/php/tests/DataHandlers/EnumDataHandlerTest.php index d652fa9..e9f3378 100644 --- a/php/tests/DataHandlers/EnumDataHandlerTest.php +++ b/php/tests/DataHandlers/EnumDataHandlerTest.php @@ -114,4 +114,79 @@ public function testMultiEnum() $h->getInput()->render() ); } + + public function testNonAssocArray() + { + $h = new EnumDataHandler(); + $h->setOptions(['1', '2', '3', '4', '5']); + $h->setValue('1'); + + self::assertEquals( + '', + $h->getInput()->render() + ); + } + + public function testAssocArray() + { + $h = new EnumDataHandler(); + $h->setOptions(['1' => 'Banana', '2' => 'Apple']); + $h->setValue('2'); + + self::assertEquals( + '', + $h->getInput()->render() + ); + } + + public function testAssocArray2() + { + $h = new EnumDataHandler(); + $h->setOptions(['Banana' => 'Banana', 'Apple' => 'Apple']); + $h->setValue('Apple'); + + self::assertEquals( + '', + $h->getInput()->render() + ); + } + + public function testNonAssocArraySplit() + { + $h = new EnumDataHandler(); + $h->setOptions(['1', '2', '3']); + $h->setValue('1'); + $h->styleSplit(); + + self::assertMatchesRegularExpression( + '~
~', + $h->getInput()->render() + ); + } + + public function testAssocArraySplit() + { + $h = new EnumDataHandler(); + $h->setOptions(['1' => 'Banana', '2' => 'Apple']); + $h->setValue('2'); + $h->styleSplit(); + + self::assertMatchesRegularExpression( + '~
~', + $h->getInput()->render() + ); + } + + public function testAssocArraySplit2() + { + $h = new EnumDataHandler(); + $h->setOptions(['Banana' => 'Banana', 'Apple' => 'Apple']); + $h->setValue('Apple'); + $h->styleSplit(); + + self::assertMatchesRegularExpression( + '~
~', + $h->getInput()->render() + ); + } }