Skip to content

Commit

Permalink
Container: onValidate does not throw warning
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 10, 2021
1 parent c0389c9 commit c6c4553
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ public function getValues($returnType = null, array $controls = null)
if ($form && $form->isSubmitted() && !$form->isValid()) {
trigger_error(__METHOD__ . '() invoked but the form is not valid.', E_USER_WARNING);
}
return $this->_getValues($returnType, $controls);
}


private function _getValues($returnType, array $controls = null)
{
if ($returnType === self::ARRAY || $returnType === true || $this->mappedType === self::ARRAY) {
$returnType = self::ARRAY;
$obj = new \stdClass;
Expand All @@ -142,7 +147,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->_getValues($type, $controls);
}
}

Expand Down Expand Up @@ -195,8 +200,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->_getValues($types[0], $controls)]
: [$this, isset($params[1]) ? $this->_getValues($types[1], $controls) : null];
$handler(...$args);
}
}
Expand Down
39 changes: 31 additions & 8 deletions tests/Forms/Forms.validationScope.phpt
Original file line number Diff line number Diff line change
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 c6c4553

Please sign in to comment.