Skip to content

Commit 48fd7a4

Browse files
committed
Merge pull request #80 from enumag/events
Fixed onError event dispatching
2 parents 6fba2ab + 81fb219 commit 48fd7a4

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/Forms/Form.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,20 @@ public function fireEvents()
409409
}
410410
}
411411

412-
if ($this->onSuccess) {
412+
if (!$this->isValid()) {
413+
$this->onError($this);
414+
} elseif ($this->onSuccess) {
413415
foreach ($this->onSuccess as $handler) {
416+
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
417+
$values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
418+
Nette\Utils\Callback::invoke($handler, $this, $values);
414419
if (!$this->isValid()) {
415420
$this->onError($this);
416421
break;
417422
}
418-
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
419-
$values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
420-
Nette\Utils\Callback::invoke($handler, $this, $values);
421423
}
422-
} elseif (!$this->isValid()) {
423-
$this->onError($this);
424424
}
425+
425426
$this->onSubmit($this);
426427
}
427428

tests/Forms/Forms.onSuccess.phpt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require __DIR__ . '/../bootstrap.php';
1414
$_SERVER['REQUEST_METHOD'] = 'POST';
1515

1616
$called = array();
17-
1817
$form = new Form;
1918
$form->addText('name');
2019
$form->addSubmit('submit');
@@ -34,8 +33,23 @@ $form->onSuccess[] = function() use (& $called) {
3433
$form->onError[] = function() use (& $called) {
3534
$called[] = 'err';
3635
};
36+
$form->fireEvents();
37+
Assert::same(array(1, 2, 'err'), $called);
3738

3839

40+
$called = array();
41+
$form = new Form;
42+
$form->addText('name');
43+
$form->addSubmit('submit');
44+
$form->onSuccess[] = function() use (& $called) {
45+
$called[] = 1;
46+
};
47+
$form->onSuccess[] = function($form) use (& $called) {
48+
$called[] = 2;
49+
$form['name']->addError('error');
50+
};
51+
$form->onError[] = function() use (& $called) {
52+
$called[] = 'err';
53+
};
3954
$form->fireEvents();
40-
4155
Assert::same(array(1, 2, 'err'), $called);

0 commit comments

Comments
 (0)