Skip to content

Commit

Permalink
added Container::getUnsafeValues(), onValidate does not throw warning [
Browse files Browse the repository at this point in the history
…Closes #266]
  • Loading branch information
dg committed Feb 10, 2021
1 parent f64c100 commit 9f513af
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
33 changes: 23 additions & 10 deletions src/Forms/Container.php
Expand Up @@ -112,20 +112,33 @@ public function setValues($data, bool $erase = false)
public function getValues($returnType = null, array $controls = null)
{
$form = $this->getForm(false);
if ($form && $form->isSubmitted() && !$form->isValid()) {
trigger_error(__METHOD__ . '() invoked but the form is not valid.', E_USER_WARNING);
if ($form && ($submitter = $form->isSubmitted())) {
if (!$this->isValid()) {
trigger_error(__METHOD__ . '() invoked but the form is not valid.', E_USER_WARNING);
}
if ($controls === null && $submitter instanceof SubmitterControl) {
$controls = $submitter->getValidationScope();
}
}
$returnType = $returnType === true ? self::ARRAY : $returnType;
return $this->getUnsafeValues($returnType, $controls);
}

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

} elseif (is_object($returnType)) {
/**
* Returns the potentially unvalidated values submitted by the form.
* @param string|object|null $returnType 'array' for array
* @param Control[]|null $controls
* @return object|array
*/
public function getUnsafeValues($returnType, array $controls = null)
{
if (is_object($returnType)) {
$obj = $returnType;

} else {
$returnType = ($returnType ?? $this->mappedType ?? ArrayHash::class);
$obj = new $returnType;
$obj = $returnType === self::ARRAY ? new \stdClass : new $returnType;
}

$rc = new \ReflectionClass($obj);
Expand All @@ -142,7 +155,7 @@ public function getValues($returnType = null, array $controls = null)
$type = $returnType === self::ARRAY && !$control->mappedType
? self::ARRAY
: ($rc->hasProperty($name) ? Nette\Utils\Reflection::getPropertyType($rc->getProperty($name)) : null);
$obj->$name = $control->getValues($type, $controls);
$obj->$name = $control->getUnsafeValues($type, $controls);
}
}

Expand Down Expand Up @@ -195,8 +208,8 @@ public function validate(array $controls = null): void
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
$types = array_map([Nette\Utils\Reflection::class, 'getParameterType'], $params);
$args = isset($types[0]) && !$this instanceof $types[0]
? [$this->getValues($types[0])]
: [$this, isset($params[1]) ? $this->getValues($types[1]) : null];
? [$this->getUnsafeValues($types[0], $controls)]
: [$this, isset($params[1]) ? $this->getUnsafeValues($types[1], $controls) : null];
$handler(...$args);
}
}
Expand Down
15 changes: 0 additions & 15 deletions src/Forms/Form.php
Expand Up @@ -374,21 +374,6 @@ public function setSubmittedBy(?SubmitterControl $by)
}


/**
* Returns the values submitted by the form.
* @param string|null $returnType 'array' for array
* @param Control[]|null $controls
* @return object|array
*/
public function getValues($returnType = null, array $controls = null)
{
if ($controls === null && $this->submittedBy instanceof SubmitterControl) {
$controls = $this->submittedBy->getValidationScope();
}
return parent::getValues($returnType, $controls);
}


/**
* Returns submitted HTTP data.
* @return mixed
Expand Down
39 changes: 31 additions & 8 deletions tests/Forms/Forms.validationScope.phpt
Expand Up @@ -13,19 +13,41 @@ use Tester\Assert;

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

//Tracy\Debugger::enable();

$datasets = [
['send1', ['container', 'form', 'name', 'age', 'age2']],
['send2', ['form']],
['send3', ['form', 'name']],
['send4', ['form', 'age']],
['send5', ['container', 'form', 'age', 'age2']],
[
'send1',
['container', 'form', 'name', 'age', 'age2'],
['name' => '', 'details' => ['age' => '', 'age2' => '']],
],
[
'send2',
['form'],
['details' => []],
],
[
'send3',
['form', 'name'],
['name' => '', 'details' => []],
],
[
'send4',
['form', 'age'],
['details' => ['age' => '']],
],
[
'send5',
['container', 'form', 'age', 'age2'],
['details' => []],
],
];

foreach ($datasets as $case) {
foreach ($datasets as $i => $case) {
$form = new Form;
$form->onValidate[] = function (Form $form) {
$res = [];
$form->onValidate[] = function (Form $form, array $vals) use (&$res) {
$form->addError('form');
$res = $vals;
};
$form->addText('name')->setRequired('name');

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

0 comments on commit 9f513af

Please sign in to comment.