Skip to content

Commit

Permalink
MultiChoiceControl: fixed order of selected items
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 29, 2025
1 parent 75efd23 commit 836743a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Forms/Controls/MultiChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ public function getItems(): array
*/
public function getSelectedItems(): array
{
$selected = array_intersect_key($this->items, array_flip($this->value));
return is_array($this->disabled)
? array_diff_key($selected, $this->disabled)
: $selected;
$res = [];
foreach ($this->value as $key) {
if (isset($this->items[$key]) && !isset($this->disabled[$key])) {
$res[$key] = $this->items[$key];
}
}
return $res;
}


Expand Down
13 changes: 13 additions & 0 deletions tests/Forms/Controls.MultiChoiceControl.loadData.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,16 @@ test('disabled one', function () use ($series) {

Assert::same([0], $input->getValue());
});


test('order of multiple items', function () {
$series = [1 => 'Ananas', 2 => 'Banana', 3 => 'Lemon'];
$_POST = ['select' => ['3', '2']];

$form = new Form;
$input = $form['select'] = new MultiChoiceControl(null, $series);

Assert::same([3, 2], $input->getValue());
Assert::same([3 => 'Lemon', 2 => 'Banana'], $input->getSelectedItems());
Assert::true($input->isFilled());
});

0 comments on commit 836743a

Please sign in to comment.