Skip to content

Commit d31a0e9

Browse files
committed
Form::getValues() returns only controls in scope (BC break)
Other controls are not validated
1 parent 39af165 commit d31a0e9

4 files changed

Lines changed: 62 additions & 5 deletions

File tree

src/Forms/Container.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public function setValues($data, bool $erase = false)
103103
/**
104104
* Returns the values submitted by the form.
105105
* @param string|object|null $returnType 'array' for array
106-
106+
* @param Control[]|null $controls
107107
* @return object|array
108108
*/
109-
public function getValues($returnType = null)
109+
public function getValues($returnType = null, array $controls = null)
110110
{
111111
if ($returnType === self::ARRAY || $returnType === true || $this->mappedType === self::ARRAY) {
112112
$returnType = self::ARRAY;
@@ -123,14 +123,18 @@ public function getValues($returnType = null)
123123
$rc = new \ReflectionClass($obj);
124124
foreach ($this->getComponents() as $name => $control) {
125125
$name = (string) $name;
126-
if ($control instanceof Control && !$control->isOmitted()) {
126+
if (
127+
$control instanceof Control
128+
&& !$control->isOmitted()
129+
&& ($controls === null || in_array($control, $controls, true))
130+
) {
127131
$obj->$name = $control->getValue();
128132

129133
} elseif ($control instanceof self) {
130134
$type = $returnType === self::ARRAY && !$control->mappedType
131135
? self::ARRAY
132136
: ($rc->hasProperty($name) ? Nette\Utils\Reflection::getPropertyType($rc->getProperty($name)) : null);
133-
$obj->$name = $control->getValues($type);
137+
$obj->$name = $control->getValues($type, $controls);
134138
}
135139
}
136140

@@ -172,7 +176,7 @@ public function isValid(): bool
172176
*/
173177
public function validate(array $controls = null): void
174178
{
175-
foreach ($controls === null ? $this->getComponents() : $controls as $control) {
179+
foreach ($controls ?? $this->getComponents() as $control) {
176180
if ($control instanceof Control || $control instanceof self) {
177181
$control->validate();
178182
}

src/Forms/Form.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,21 @@ public function setSubmittedBy(?SubmitterControl $by)
358358
}
359359

360360

361+
/**
362+
* Returns the values submitted by the form.
363+
* @param string|null $returnType 'array' for array
364+
* @param Control[]|null $controls
365+
* @return object|array
366+
*/
367+
public function getValues($returnType = null, array $controls = null)
368+
{
369+
if ($controls === null && $this->submittedBy instanceof SubmitterControl) {
370+
$controls = $this->submittedBy->getValidationScope();
371+
}
372+
return parent::getValues($returnType, $controls);
373+
}
374+
375+
361376
/**
362377
* Returns submitted HTTP data.
363378
* @return mixed

tests/Forms/Container.values.array.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,22 @@ test('onSuccess test', function () {
256256
$form->fireEvents();
257257
Assert::true($ok);
258258
});
259+
260+
261+
test('submitted form + setValidationScope() + getValues(true)', function () {
262+
$_SERVER['REQUEST_METHOD'] = 'POST';
263+
$_POST['send'] = '';
264+
265+
$form = createForm();
266+
$form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['second']['city']]);
267+
268+
Assert::truthy($form->isSubmitted());
269+
Assert::equal([
270+
'title' => 'sent title',
271+
'first' => [
272+
'second' => [
273+
'city' => 'sent city',
274+
],
275+
],
276+
], $form->getValues(true));
277+
});

tests/Forms/Container.values.mapping.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,22 @@ test('getValues() + object', function () {
335335

336336
Assert::same($obj, $orig);
337337
});
338+
339+
340+
test('submitted form + setValidationScope() + getValues(true)', function () {
341+
$_SERVER['REQUEST_METHOD'] = 'POST';
342+
$_POST['send'] = '';
343+
344+
$form = createForm();
345+
$form->addSubmit('send')->setValidationScope([$form['title'], $form['first']['second']['city']]);
346+
347+
Assert::truthy($form->isSubmitted());
348+
Assert::equal(hydrate(FormData::class, [
349+
'title' => 'sent title',
350+
'first' => ArrayHash::from([
351+
'second' => ArrayHash::from([
352+
'city' => 'sent city',
353+
]),
354+
]),
355+
]), $form->getValues(FormData::class));
356+
});

0 commit comments

Comments
 (0)