Skip to content

Commit aab23cc

Browse files
committed
added Container::getUnsafeValues(), onValidate does not throw warning [Closes #266]
1 parent f64c100 commit aab23cc

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

src/Forms/Container.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,28 @@ public function setValues($data, bool $erase = false)
112112
public function getValues($returnType = null, array $controls = null)
113113
{
114114
$form = $this->getForm(false);
115-
if ($form && $form->isSubmitted() && !$form->isValid()) {
115+
if ($form && $form->isSubmitted() && !$this->isValid()) {
116116
trigger_error(__METHOD__ . '() invoked but the form is not valid.', E_USER_WARNING);
117117
}
118+
$returnType = $returnType === true ? self::ARRAY : $returnType;
119+
return $this->getUnsafeValues($returnType, $controls);
120+
}
118121

119-
if ($returnType === self::ARRAY || $returnType === true || $this->mappedType === self::ARRAY) {
120-
$returnType = self::ARRAY;
121-
$obj = new \stdClass;
122122

123-
} elseif (is_object($returnType)) {
123+
/**
124+
* Returns the potentially unvalidated values submitted by the form.
125+
* @param string|object|null $returnType 'array' for array
126+
* @param Control[]|null $controls
127+
* @return object|array
128+
*/
129+
public function getUnsafeValues($returnType, array $controls = null)
130+
{
131+
if (is_object($returnType)) {
124132
$obj = $returnType;
125133

126134
} else {
127135
$returnType = ($returnType ?? $this->mappedType ?? ArrayHash::class);
128-
$obj = new $returnType;
136+
$obj = $returnType === self::ARRAY ? new \stdClass : new $returnType;
129137
}
130138

131139
$rc = new \ReflectionClass($obj);
@@ -142,7 +150,7 @@ public function getValues($returnType = null, array $controls = null)
142150
$type = $returnType === self::ARRAY && !$control->mappedType
143151
? self::ARRAY
144152
: ($rc->hasProperty($name) ? Nette\Utils\Reflection::getPropertyType($rc->getProperty($name)) : null);
145-
$obj->$name = $control->getValues($type, $controls);
153+
$obj->$name = $control->getUnsafeValues($type, $controls);
146154
}
147155
}
148156

@@ -195,8 +203,8 @@ public function validate(array $controls = null): void
195203
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
196204
$types = array_map([Nette\Utils\Reflection::class, 'getParameterType'], $params);
197205
$args = isset($types[0]) && !$this instanceof $types[0]
198-
? [$this->getValues($types[0])]
199-
: [$this, isset($params[1]) ? $this->getValues($types[1]) : null];
206+
? [$this->getUnsafeValues($types[0], $controls)]
207+
: [$this, isset($params[1]) ? $this->getUnsafeValues($types[1], $controls) : null];
200208
$handler(...$args);
201209
}
202210
}

tests/Forms/Forms.validationScope.phpt

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,41 @@ use Tester\Assert;
1313

1414
require __DIR__ . '/../bootstrap.php';
1515

16-
//Tracy\Debugger::enable();
16+
1717
$datasets = [
18-
['send1', ['container', 'form', 'name', 'age', 'age2']],
19-
['send2', ['form']],
20-
['send3', ['form', 'name']],
21-
['send4', ['form', 'age']],
22-
['send5', ['container', 'form', 'age', 'age2']],
18+
[
19+
'send1',
20+
['container', 'form', 'name', 'age', 'age2'],
21+
['name' => '', 'details' => ['age' => '', 'age2' => '']],
22+
],
23+
[
24+
'send2',
25+
['form'],
26+
['details' => []],
27+
],
28+
[
29+
'send3',
30+
['form', 'name'],
31+
['name' => '', 'details' => []],
32+
],
33+
[
34+
'send4',
35+
['form', 'age'],
36+
['details' => ['age' => '']],
37+
],
38+
[
39+
'send5',
40+
['container', 'form', 'age', 'age2'],
41+
['details' => []],
42+
],
2343
];
2444

25-
foreach ($datasets as $case) {
45+
foreach ($datasets as $i => $case) {
2646
$form = new Form;
27-
$form->onValidate[] = function (Form $form) {
47+
$res = [];
48+
$form->onValidate[] = function (Form $form, array $vals) use (&$res) {
2849
$form->addError('form');
50+
$res = $vals;
2951
};
3052
$form->addText('name')->setRequired('name');
3153

@@ -47,4 +69,5 @@ foreach ($datasets as $case) {
4769
Assert::truthy($form->isSubmitted());
4870
$form->validate();
4971
Assert::equal($case[1], $form->getErrors());
72+
Assert::equal($case[2] ?? [], $res);
5073
}

0 commit comments

Comments
 (0)