From c2285dc4d26c600d41813c34ec8c200e545f0b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Brecher?= <34324008+mildabre@users.noreply.github.com> Date: Sat, 27 Apr 2024 13:35:24 +0200 Subject: [PATCH] Container::setDefaults() fixed setting value to disabled elements setDefaults(), unlike setDefaultValue(), did not set the value to form element when it was disabled by setDisabled() after the anchor event. --- src/Forms/Container.php | 11 ++++------- tests/Forms/Controls.BaseControl.phpt | 4 ++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Forms/Container.php b/src/Forms/Container.php index 9c2946ae6..05782623b 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 4aacf5fdc..ce8ede69e 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;