diff --git a/src/Forms/Container.php b/src/Forms/Container.php index 9c2946ae..05782623 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -49,10 +49,7 @@ class Container extends Nette\ComponentModel\Container implements \ArrayAccess public function setDefaults(array|object $data, bool $erase = false): static { $form = $this->getForm(false); - if (!$form || !$form->isAnchored() || !$form->isSubmitted()) { - $this->setValues($data, $erase); - } - + $this->setValues($data, $erase, $form?->isAnchored() && $form->isSubmitted()); return $this; } @@ -61,7 +58,7 @@ public function setDefaults(array|object $data, bool $erase = false): static * Fill-in with values. * @internal */ - public function setValues(array|object $values, bool $erase = false): static + public function setValues(array|object $values, bool $erase = false, bool $onlyDisabled = false): static { $values = $values instanceof \Traversable ? iterator_to_array($values) @@ -69,12 +66,12 @@ public function setValues(array|object $values, bool $erase = false): static foreach ($this->getComponents() as $name => $control) { if ($control instanceof Control) { - if (array_key_exists($name, $values) || $erase) { + if ((array_key_exists($name, $values) && (!$onlyDisabled || $control->isDisabled())) || $erase) { $control->setValue($values[$name] ?? null); } } elseif ($control instanceof self) { if (isset($values[$name]) || $erase) { - $control->setValues($values[$name] ?? [], $erase); + $control->setValues($values[$name] ?? [], $erase, $onlyDisabled); } } } diff --git a/tests/Forms/Controls.BaseControl.phpt b/tests/Forms/Controls.BaseControl.phpt index 4aacf5fd..ce8ede69 100644 --- a/tests/Forms/Controls.BaseControl.phpt +++ b/tests/Forms/Controls.BaseControl.phpt @@ -137,9 +137,13 @@ test('disabled & submitted', function () { $form->addText('disabled') ->setDisabled() ->setDefaultValue('default'); + $form->addText('disabled2') + ->setDisabled(); + $form->setDefaults(['disabled2' => 'default']); Assert::true($form->isSubmitted()); Assert::same('default', $form['disabled']->getValue()); + Assert::same('default', $form['disabled2']->getValue()); unset($form['disabled']); $input = new Nette\Forms\Controls\TextInput;