Skip to content

Commit 3a6b571

Browse files
committed
ChoiceControl, MultiChoiceControl: disabled items are processed in getValue() instead of loadHttpData()
1 parent eb77349 commit 3a6b571

File tree

3 files changed

+10
-21
lines changed

3 files changed

+10
-21
lines changed

src/Forms/Controls/CheckboxList.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ public function loadHttpData(): void
4646
? $this->getHttpData(Nette\Forms\Form::DataText)
4747
: explode(',', $data);
4848
$this->value = array_keys(array_flip($data));
49-
if (is_array($this->disabled)) {
50-
$this->value = array_diff($this->value, array_keys($this->disabled));
51-
}
5249
}
5350

5451

src/Forms/Controls/ChoiceControl.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@ public function __construct($label = null, ?array $items = null)
3838

3939
public function loadHttpData(): void
4040
{
41-
$this->value = $this->getHttpData(Nette\Forms\Form::DataText);
42-
if ($this->value !== null) {
43-
$this->value = is_array($this->disabled) && isset($this->disabled[$this->value])
44-
? null
45-
: key([$this->value => null]);
46-
}
41+
$value = $this->getHttpData(Nette\Forms\Form::DataText);
42+
$this->value = $value === null ? null : Arrays::toKey($value);
4743
}
4844

4945

@@ -81,7 +77,9 @@ public function setValue($value)
8177
*/
8278
public function getValue(): mixed
8379
{
84-
return $this->value !== null && ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
80+
return $this->value !== null
81+
&& !isset($this->disabled[$this->value])
82+
&& ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
8583
? $res
8684
: null;
8785
}
@@ -133,7 +131,9 @@ public function getItems(): array
133131
*/
134132
public function getSelectedItem(): mixed
135133
{
136-
return $this->value !== null && ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
134+
return $this->value !== null
135+
&& !isset($this->disabled[$this->value])
136+
&& ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
137137
? $res
138138
: null;
139139
}
@@ -150,10 +150,6 @@ public function setDisabled(bool|array $value = true): static
150150

151151
parent::setDisabled(false);
152152
$this->disabled = array_fill_keys($value, value: true);
153-
if (isset($this->disabled[$this->value])) {
154-
$this->value = null;
155-
}
156-
157153
return $this;
158154
}
159155

src/Forms/Controls/MultiChoiceControl.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ public function __construct($label = null, ?array $items = null)
3939
public function loadHttpData(): void
4040
{
4141
$this->value = array_keys(array_flip($this->getHttpData(Nette\Forms\Form::DataText)));
42-
if (is_array($this->disabled)) {
43-
$this->value = array_diff($this->value, array_keys($this->disabled));
44-
}
4542
}
4643

4744

@@ -86,7 +83,7 @@ public function setValue($values)
8683
*/
8784
public function getValue(): array
8885
{
89-
return array_values(array_intersect($this->value, array_column($this->choices, 0)));
86+
return array_keys($this->getSelectedItems());
9087
}
9188

9289

@@ -128,7 +125,7 @@ public function getItems(): array
128125
public function getSelectedItems(): array
129126
{
130127
$flip = array_flip($this->value);
131-
$res = array_filter($this->choices, fn($choice) => isset($flip[$choice[0]]));
128+
$res = array_filter($this->choices, fn($choice) => isset($flip[$choice[0]]) && !isset($this->disabled[$choice[0]]));
132129
return array_column($res, 1, 0);
133130
}
134131

@@ -144,7 +141,6 @@ public function setDisabled(bool|array $value = true): static
144141

145142
parent::setDisabled(false);
146143
$this->disabled = array_fill_keys($value, value: true);
147-
$this->value = array_diff($this->value, $value);
148144
return $this;
149145
}
150146

0 commit comments

Comments
 (0)