Skip to content

Commit

Permalink
Form::getValues() fixed when in validation scope is container
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 24, 2021
1 parent 54858a9 commit e412326
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,20 @@ 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();

} elseif ($control instanceof self) {
$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);
}
}

Expand Down
20 changes: 19 additions & 1 deletion tests/Forms/Container.values.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
20 changes: 19 additions & 1 deletion tests/Forms/Container.values.mapping.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down

0 comments on commit e412326

Please sign in to comment.