diff --git a/src/Forms/Controls/CheckboxList.php b/src/Forms/Controls/CheckboxList.php index 8b2bcc41..2178f4e7 100644 --- a/src/Forms/Controls/CheckboxList.php +++ b/src/Forms/Controls/CheckboxList.php @@ -46,9 +46,6 @@ public function loadHttpData(): void ? $this->getHttpData(Nette\Forms\Form::DataText) : explode(',', $data); $this->value = array_keys(array_flip($data)); - if (is_array($this->disabled)) { - $this->value = array_diff($this->value, array_keys($this->disabled)); - } } diff --git a/src/Forms/Controls/ChoiceControl.php b/src/Forms/Controls/ChoiceControl.php index c3ece118..737e580f 100644 --- a/src/Forms/Controls/ChoiceControl.php +++ b/src/Forms/Controls/ChoiceControl.php @@ -38,12 +38,8 @@ public function __construct($label = null, ?array $items = null) public function loadHttpData(): void { - $this->value = $this->getHttpData(Nette\Forms\Form::DataText); - if ($this->value !== null) { - $this->value = is_array($this->disabled) && isset($this->disabled[$this->value]) - ? null - : key([$this->value => null]); - } + $value = $this->getHttpData(Nette\Forms\Form::DataText); + $this->value = $value === null ? null : Arrays::toKey($value); } @@ -81,7 +77,9 @@ public function setValue($value) */ public function getValue(): mixed { - return $this->value !== null && ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value)) + return $this->value !== null + && !isset($this->disabled[$this->value]) + && ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value)) ? $res : null; } @@ -133,7 +131,9 @@ public function getItems(): array */ public function getSelectedItem(): mixed { - return $this->value !== null && ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value)) + return $this->value !== null + && !isset($this->disabled[$this->value]) + && ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value)) ? $res : null; } @@ -150,10 +150,6 @@ public function setDisabled(bool|array $value = true): static parent::setDisabled(false); $this->disabled = array_fill_keys($value, value: true); - if (isset($this->disabled[$this->value])) { - $this->value = null; - } - return $this; } diff --git a/src/Forms/Controls/MultiChoiceControl.php b/src/Forms/Controls/MultiChoiceControl.php index 229e6974..ea28f118 100644 --- a/src/Forms/Controls/MultiChoiceControl.php +++ b/src/Forms/Controls/MultiChoiceControl.php @@ -39,9 +39,6 @@ public function __construct($label = null, ?array $items = null) public function loadHttpData(): void { $this->value = array_keys(array_flip($this->getHttpData(Nette\Forms\Form::DataText))); - if (is_array($this->disabled)) { - $this->value = array_diff($this->value, array_keys($this->disabled)); - } } @@ -86,7 +83,7 @@ public function setValue($values) */ public function getValue(): array { - return array_values(array_intersect($this->value, array_column($this->choices, 0))); + return array_keys($this->getSelectedItems()); } @@ -128,7 +125,7 @@ public function getItems(): array public function getSelectedItems(): array { $flip = array_flip($this->value); - $res = array_filter($this->choices, fn($choice) => isset($flip[$choice[0]])); + $res = array_filter($this->choices, fn($choice) => isset($flip[$choice[0]]) && !isset($this->disabled[$choice[0]])); return array_column($res, 1, 0); } @@ -144,7 +141,6 @@ public function setDisabled(bool|array $value = true): static parent::setDisabled(false); $this->disabled = array_fill_keys($value, value: true); - $this->value = array_diff($this->value, $value); return $this; }