Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
18 changes: 16 additions & 2 deletions tests/Forms/Forms.onSuccess.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require __DIR__ . '/../bootstrap.php';
$_SERVER['REQUEST_METHOD'] = 'POST';

$called = array();

$form = new Form;
$form->addText('name');
$form->addSubmit('submit');
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tady ten by tu být neměl aby se ověřilo že se to zavolá ikdyž je tam jenom jeden onSuccess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ono se to nevolalo pokud error nastal v posledním onSuccess handleru - jejich celkový počet nemá vliv, tedy je jedno zda tenhle onSuccess odstraním či nikoli

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pravda :)

$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);