Skip to content

Commit

Permalink
Container::getValues() respects validation scope [Closes #287]
Browse files Browse the repository at this point in the history
  • Loading branch information
norbe authored and dg committed Oct 10, 2023
1 parent d8fa670 commit cc85ffd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ public function getValues($returnType = null, ?array $controls = null)

if ($controls === null && $submitter instanceof SubmitterControl) {
$controls = $submitter->getValidationScope();
if ($controls !== null && !in_array($this, $controls, true)) {
$scope = $this;
while (($scope = $scope->getParent()) instanceof self) {
if (in_array($scope, $controls, true)) {
$controls[] = $this;
break;
}
}
}
}
}

Expand Down Expand Up @@ -155,7 +164,7 @@ public function getUntrustedValues($returnType = ArrayHash::class, ?array $contr
}

foreach ($this->getComponents() as $name => $control) {
$allowed = $controls === null || in_array($control, $controls, true);
$allowed = $controls === null || in_array($this, $controls, true) || in_array($control, $controls, true);
$name = (string) $name;
if (
$control instanceof Control
Expand Down
17 changes: 17 additions & 0 deletions tests/Forms/Container.values.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,23 @@ test('submitted form + setValidationScope() + getValues(true)', function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['send'] = '';

$form = createForm();
$form->addSubmit('send')->setValidationScope([$form['first']]);

Assert::truthy($form->isSubmitted());
Assert::equal([
'name' => '',
'age' => 999,
'second' => [
'city' => 'sent city',
],
], $form['first']->getValues('array'));
});

test('submitted form + setValidationScope() + getValues(array)', function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['send'] = '';

$form = createForm();
$form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['second']]);

Expand Down

0 comments on commit cc85ffd

Please sign in to comment.