From 9650adea947a99ba689765d02e77390b0d9253fd Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 4 Feb 2021 03:36:34 +0100 Subject: [PATCH] Revert "Form::onValidate - values are passed only when form is valid" This reverts commit b67ff3fcca97a2b0d8688b652ded22fec4736932. --- src/Forms/Container.php | 13 ++++--------- tests/Forms/Forms.validationScope.phpt | 22 ++++++++-------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/Forms/Container.php b/src/Forms/Container.php index 496f36bfc..96043ad45 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -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); } } diff --git a/tests/Forms/Forms.validationScope.phpt b/tests/Forms/Forms.validationScope.phpt index 88402cea3..d33b96d4d 100644 --- a/tests/Forms/Forms.validationScope.phpt +++ b/tests/Forms/Forms.validationScope.phpt @@ -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']]); @@ -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); }