Skip to content

Commit 0d267a4

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

3 files changed

Lines changed: 54 additions & 33 deletions

File tree

src/Forms/Container.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,33 @@ 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()) {
116-
trigger_error(__METHOD__ . '() invoked but the form is not valid.', E_USER_WARNING);
115+
if ($form && ($submitter = $form->isSubmitted())) {
116+
if (!$this->isValid()) {
117+
trigger_error(__METHOD__ . '() invoked but the form is not valid.', E_USER_WARNING);
118+
}
119+
if ($controls === null && $submitter instanceof SubmitterControl) {
120+
$controls = $submitter->getValidationScope();
121+
}
117122
}
123+
$returnType = $returnType === true ? self::ARRAY : $returnType;
124+
return $this->getUnsafeValues($returnType, $controls);
125+
}
118126

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

123-
} elseif (is_object($returnType)) {
128+
/**
129+
* Returns the potentially unvalidated values submitted by the form.
130+
* @param string|object|null $returnType 'array' for array
131+
* @param Control[]|null $controls
132+
* @return object|array
133+
*/
134+
public function getUnsafeValues($returnType, array $controls = null)
135+
{
136+
if (is_object($returnType)) {
124137
$obj = $returnType;
125138

126139
} else {
127140
$returnType = ($returnType ?? $this->mappedType ?? ArrayHash::class);
128-
$obj = new $returnType;
141+
$obj = $returnType === self::ARRAY ? new \stdClass : new $returnType;
129142
}
130143

131144
$rc = new \ReflectionClass($obj);
@@ -142,7 +155,7 @@ public function getValues($returnType = null, array $controls = null)
142155
$type = $returnType === self::ARRAY && !$control->mappedType
143156
? self::ARRAY
144157
: ($rc->hasProperty($name) ? Nette\Utils\Reflection::getPropertyType($rc->getProperty($name)) : null);
145-
$obj->$name = $control->getValues($type, $controls);
158+
$obj->$name = $control->getUnsafeValues($type, $controls);
146159
}
147160
}
148161

@@ -195,8 +208,8 @@ public function validate(array $controls = null): void
195208
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
196209
$types = array_map([Nette\Utils\Reflection::class, 'getParameterType'], $params);
197210
$args = isset($types[0]) && !$this instanceof $types[0]
198-
? [$this->getValues($types[0])]
199-
: [$this, isset($params[1]) ? $this->getValues($types[1]) : null];
211+
? [$this->getUnsafeValues($types[0], $controls)]
212+
: [$this, isset($params[1]) ? $this->getUnsafeValues($types[1], $controls) : null];
200213
$handler(...$args);
201214
}
202215
}

src/Forms/Form.php

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

376376

377-
/**
378-
* Returns the values submitted by the form.
379-
* @param string|null $returnType 'array' for array
380-
* @param Control[]|null $controls
381-
* @return object|array
382-
*/
383-
public function getValues($returnType = null, array $controls = null)
384-
{
385-
if ($controls === null && $this->submittedBy instanceof SubmitterControl) {
386-
$controls = $this->submittedBy->getValidationScope();
387-
}
388-
return parent::getValues($returnType, $controls);
389-
}
390-
391-
392377
/**
393378
* Returns submitted HTTP data.
394379
* @return mixed

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)