diff --git a/src/Forms/Container.php b/src/Forms/Container.php index 819ea57d4..c19e09fd7 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -143,11 +143,12 @@ public function getUnsafeValues($returnType, array $controls = null) $rc = new \ReflectionClass($obj); foreach ($this->getComponents() as $name => $control) { + $allowed = $controls === null || in_array($control, $controls, true); $name = (string) $name; if ( $control instanceof Control + && $allowed && !$control->isOmitted() - && ($controls === null || in_array($control, $controls, true)) ) { $obj->$name = $control->getValue(); @@ -155,7 +156,7 @@ public function getUnsafeValues($returnType, array $controls = null) $type = $returnType === self::ARRAY && !$control->mappedType ? self::ARRAY : ($rc->hasProperty($name) ? Nette\Utils\Reflection::getPropertyType($rc->getProperty($name)) : null); - $obj->$name = $control->getUnsafeValues($type, $controls); + $obj->$name = $control->getUnsafeValues($type, $allowed ? null : $controls); } } diff --git a/tests/Forms/Container.values.array.phpt b/tests/Forms/Container.values.array.phpt index 428a4abad..417f2259d 100644 --- a/tests/Forms/Container.values.array.phpt +++ b/tests/Forms/Container.values.array.phpt @@ -266,7 +266,25 @@ test('submitted form + setValidationScope() + getValues(true)', function () { $_POST['send'] = ''; $form = createForm(); - $form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['second']['city']]); + $form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['age']]); + + Assert::truthy($form->isSubmitted()); + Assert::equal([ + 'title' => 'sent title', + 'first' => [ + 'age' => 999, + 'second' => [], + ], + ], $form->getValues(true)); +}); + + +test('submitted form + setValidationScope() + getValues(true)', function () { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['send'] = ''; + + $form = createForm(); + $form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['second']]); Assert::truthy($form->isSubmitted()); Assert::equal([ diff --git a/tests/Forms/Container.values.mapping.phpt b/tests/Forms/Container.values.mapping.phpt index 8f5951ac2..73367dad2 100644 --- a/tests/Forms/Container.values.mapping.phpt +++ b/tests/Forms/Container.values.mapping.phpt @@ -345,7 +345,25 @@ test('submitted form + setValidationScope() + getValues(true)', function () { $_POST['send'] = ''; $form = createForm(); - $form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['second']['city']]); + $form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['age']]); + + Assert::truthy($form->isSubmitted()); + Assert::equal(hydrate(FormData::class, [ + 'title' => 'sent title', + 'first' => ArrayHash::from([ + 'age' => 999, + 'second' => ArrayHash::from([]), + ]), + ]), $form->getValues(FormData::class)); +}); + + +test('submitted form + setValidationScope() + getValues(true)', function () { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['send'] = ''; + + $form = createForm(); + $form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['second']]); Assert::truthy($form->isSubmitted()); Assert::equal(hydrate(FormData::class, [