diff --git a/src/Forms/Controls/MultiChoiceControl.php b/src/Forms/Controls/MultiChoiceControl.php index 3f2ef3a7..66f537f2 100644 --- a/src/Forms/Controls/MultiChoiceControl.php +++ b/src/Forms/Controls/MultiChoiceControl.php @@ -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; } diff --git a/tests/Forms/Controls.MultiChoiceControl.loadData.phpt b/tests/Forms/Controls.MultiChoiceControl.loadData.phpt index 8c1dff86..3f11eb54 100644 --- a/tests/Forms/Controls.MultiChoiceControl.loadData.phpt +++ b/tests/Forms/Controls.MultiChoiceControl.loadData.phpt @@ -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()); +});