From 81fb219641db4f43f00176c1479bd3dfd12e9985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Tou=C5=A1ek?= Date: Mon, 20 Apr 2015 22:40:09 +0200 Subject: [PATCH] Form::fireEvents() calls onError even after last onSuccess handler --- src/Forms/Form.php | 13 +++++++------ tests/Forms/Forms.onSuccess.phpt | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 17444719c..b90acb4ac 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -409,19 +409,20 @@ public function fireEvents() } } - if ($this->onSuccess) { + if (!$this->isValid()) { + $this->onError($this); + } elseif ($this->onSuccess) { foreach ($this->onSuccess as $handler) { + $params = Nette\Utils\Callback::toReflection($handler)->getParameters(); + $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL; + Nette\Utils\Callback::invoke($handler, $this, $values); if (!$this->isValid()) { $this->onError($this); break; } - $params = Nette\Utils\Callback::toReflection($handler)->getParameters(); - $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL; - Nette\Utils\Callback::invoke($handler, $this, $values); } - } elseif (!$this->isValid()) { - $this->onError($this); } + $this->onSubmit($this); } diff --git a/tests/Forms/Forms.onSuccess.phpt b/tests/Forms/Forms.onSuccess.phpt index 2b78d0599..b2cd358e8 100644 --- a/tests/Forms/Forms.onSuccess.phpt +++ b/tests/Forms/Forms.onSuccess.phpt @@ -14,7 +14,6 @@ require __DIR__ . '/../bootstrap.php'; $_SERVER['REQUEST_METHOD'] = 'POST'; $called = array(); - $form = new Form; $form->addText('name'); $form->addSubmit('submit'); @@ -34,8 +33,23 @@ $form->onSuccess[] = function() use (& $called) { $form->onError[] = function() use (& $called) { $called[] = 'err'; }; +$form->fireEvents(); +Assert::same(array(1, 2, 'err'), $called); +$called = array(); +$form = new Form; +$form->addText('name'); +$form->addSubmit('submit'); +$form->onSuccess[] = function() use (& $called) { + $called[] = 1; +}; +$form->onSuccess[] = function($form) use (& $called) { + $called[] = 2; + $form['name']->addError('error'); +}; +$form->onError[] = function() use (& $called) { + $called[] = 'err'; +}; $form->fireEvents(); - Assert::same(array(1, 2, 'err'), $called);