Skip to content

Commit

Permalink
Add functionality for the multiple attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Nov 8, 2022
1 parent 194fd78 commit e71d5f3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/FormElement/SelectElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Html\FormElement;

use Closure;
use ipl\Html\Attributes;
use ipl\Html\Html;
use ipl\Validator\DeferredInArrayValidator;

Expand All @@ -21,8 +22,20 @@ class SelectElement extends BaseFormElement
/** @var array Disabled values */
protected $disabledOptions = [];

/** @var array|string Array when the attribute 'multiple' is set to true, string otherwise */
protected $value;

/** @var bool Whether the attribute 'multiple' is set to true */
protected $isMultiSelect;

public function __construct($name, $attributes = null)
{
if (is_array($attributes)) { // because attributes can be in any order
$this->isMultiSelect = in_array('multiple', $attributes) && $attributes['multiple'] === true;
} elseif ($attributes instanceof Attributes) {
$this->isMultiSelect = $attributes->get('multiple')->getValue() === true;
}

$this->getAttributes()->registerAttributeCallback(
'options',
null,
Expand All @@ -44,6 +57,24 @@ public function getValueAttribute()
return null;
}

public function getNameAttribute()
{
if ($this->isMultiSelect) {
return $this->getName() . '[]';
}

return $this->getName();
}

public function setValue($value)
{
if ($this->isMultiSelect) {
$value = empty($value) ? [] : (array) $value;
}

return parent::setValue($value);
}

public function hasOption($value)
{
return isset($this->options[$value]);
Expand Down Expand Up @@ -143,6 +174,10 @@ protected function makeOption($value, $label)
*/
protected function isSelectedOption($optionValue)
{
if ($this->isMultiSelect) {
return in_array($optionValue, (array)$this->getValue(), ! is_int($optionValue));
}

return is_int($optionValue)
// The loose comparison is required because PHP casts
// numeric strings to integers if used as array keys
Expand Down
73 changes: 73 additions & 0 deletions tests/FormElement/SelectElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,53 @@ public function testDefaultValueIsSelected()
);
}

public function testSelectingMultipleOptions()
{
$select = new SelectElement('elname', [

'value' => '4',
'multiple' => true,
'options' => [
null => 'Please choose',
'1' => 'The one',
'4' => 'Four',
'5' => 'Hi five',
'sub' => [
'Down' => 'Here'
]
]
]);

$this->assertEquals($select->getValue(), [4]);
$select->setValue([1, '5', 'Down']);
$this->assertEquals($select->getValue(), [1, '5', 'Down']);
}

public function testMultiDefaultValuesAreSelected()
{
$select = new SelectElement('elname', [
'multiple' => true,
'value' => ['1', '5'],
'options' => [
null => 'Please choose',
'1' => 'The one',
'4' => 'Four',
'5' => 'Hi five'
]
]);

$html = <<<HTML
<select name="elname[]" multiple>
<option value="">Please choose</option>
<option selected value="1">The one</option>
<option value="4">Four</option>
<option selected value="5">Hi five</option>
</select>
HTML;

$this->assertHtml($html, $select);
}

public function testSetValueSelectsAnOption()
{
$select = new SelectElement('elname', [
Expand Down Expand Up @@ -272,4 +319,30 @@ public function testLabelCanBeChanged()
$option->setLabel('New label');
$this->assertHtml('<option value="value">New label</option>', $option);
}

public function testSetValueSelectsMultipleOptions()
{
$select = new SelectElement('elname', [
'multiple' => true,
'options' => [
null => 'Please choose',
'1' => 'The one',
'4' => 'Four',
'5' => 'Hi five'
]
]);

$select->setValue(['1', 4]);

$html = <<<HTML
<select name="elname[]" multiple>
<option value="">Please choose</option>
<option selected value="1">The one</option>
<option selected value="4">Four</option>
<option value="5">Hi five</option>
</select>
HTML;

$this->assertHtml($html, $select);
}
}

0 comments on commit e71d5f3

Please sign in to comment.