Skip to content

Commit cc85ffd

Browse files
norbedg
authored andcommitted
Container::getValues() respects validation scope [Closes #287]
1 parent d8fa670 commit cc85ffd

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Forms/Container.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ public function getValues($returnType = null, ?array $controls = null)
123123

124124
if ($controls === null && $submitter instanceof SubmitterControl) {
125125
$controls = $submitter->getValidationScope();
126+
if ($controls !== null && !in_array($this, $controls, true)) {
127+
$scope = $this;
128+
while (($scope = $scope->getParent()) instanceof self) {
129+
if (in_array($scope, $controls, true)) {
130+
$controls[] = $this;
131+
break;
132+
}
133+
}
134+
}
126135
}
127136
}
128137

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

157166
foreach ($this->getComponents() as $name => $control) {
158-
$allowed = $controls === null || in_array($control, $controls, true);
167+
$allowed = $controls === null || in_array($this, $controls, true) || in_array($control, $controls, true);
159168
$name = (string) $name;
160169
if (
161170
$control instanceof Control

tests/Forms/Container.values.array.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,23 @@ test('submitted form + setValidationScope() + getValues(true)', function () {
283283
$_SERVER['REQUEST_METHOD'] = 'POST';
284284
$_POST['send'] = '';
285285

286+
$form = createForm();
287+
$form->addSubmit('send')->setValidationScope([$form['first']]);
288+
289+
Assert::truthy($form->isSubmitted());
290+
Assert::equal([
291+
'name' => '',
292+
'age' => 999,
293+
'second' => [
294+
'city' => 'sent city',
295+
],
296+
], $form['first']->getValues('array'));
297+
});
298+
299+
test('submitted form + setValidationScope() + getValues(array)', function () {
300+
$_SERVER['REQUEST_METHOD'] = 'POST';
301+
$_POST['send'] = '';
302+
286303
$form = createForm();
287304
$form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['second']]);
288305

0 commit comments

Comments
 (0)