Skip to content

Commit

Permalink
Revert "Form::onValidate - values are passed only when form is valid"
Browse files Browse the repository at this point in the history
This reverts commit b67ff3f.
  • Loading branch information
dg committed Feb 4, 2021
1 parent 5c92a63 commit 9650ade
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
13 changes: 4 additions & 9 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,13 @@ public function validate(array $controls = null): void
}
$this->validated = true;

$isValid = !$this->getErrors();
foreach ($this->onValidate as $handler) {
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
$types = array_map([Nette\Utils\Reflection::class, 'getParameterType'], $params);
$handler(
!isset($types[0]) || $this instanceof $types[0]
? $this
: ($isValid ? $this->getValues($types[0]) : null),
isset($params[1]) && $isValid
? $this->getValues($types[1])
: null
);
$args = isset($types[0]) && !$this instanceof $types[0]
? [$this->getValues($types[0])]
: [$this, isset($params[1]) ? $this->getValues($types[1]) : null];
$handler(...$args);
}
}

Expand Down
22 changes: 8 additions & 14 deletions tests/Forms/Forms.validationScope.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,29 @@ require __DIR__ . '/../bootstrap.php';

//Tracy\Debugger::enable();
$datasets = [
['send1', ['container', 'form', 'name', 'age', 'age2'], null],
['send2', ['form'], ['optional' => '', 'details' => []]],
['send3', ['form', 'name'], null],
['send4', ['form', 'age'], null],
['send5', ['container', 'form', 'age', 'age2'], null],
['send1', ['container', 'form', 'name', 'age', 'age2']],
['send2', ['form']],
['send3', ['form', 'name']],
['send4', ['form', 'age']],
['send5', ['container', 'form', 'age', 'age2']],
];

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

$details = $form->addContainer('details');
$details->onValidate[] = function (Container $container, $values) use (&$values2) {
$details->onValidate[] = function (Container $container) {
$container->getForm()->addError('container');
$values2 = $values;
};
$details->addText('age')->setRequired('age');
$details->addText('age2')->setRequired('age2');

$form->addSubmit('send1');
$form->addSubmit('send2')->setValidationScope([$form['optional']]);
$form->addSubmit('send2')->setValidationScope([]);
$form->addSubmit('send3')->setValidationScope([$form['name']]);
$form->addSubmit('send4')->setValidationScope([$form['details']['age']]);
$form->addSubmit('send5')->setValidationScope([$form['details']]);
Expand All @@ -50,7 +47,4 @@ foreach ($datasets as $case) {
Assert::truthy($form->isSubmitted());
$form->validate();
Assert::equal($case[1], $form->getErrors());

Assert::same($case[2], $values1);
Assert::null($values2);
}

0 comments on commit 9650ade

Please sign in to comment.