Skip to content

Commit

Permalink
Form: checks the existence of handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 7, 2021
1 parent bfbef39 commit 842cb1e
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,10 @@ public function fireEvents(): void
$this->validate();
}

$handled = count($this->onSuccess ?? []) || count($this->onSubmit ?? []);

if ($this->submittedBy instanceof Controls\SubmitButton) {
$handled = $handled || count($this->submittedBy->onClick ?? []);
if ($this->isValid()) {
$this->invokeHandlers($this->submittedBy->onClick, $this->submittedBy);
} else {
Expand All @@ -427,6 +430,10 @@ public function fireEvents(): void
}

Arrays::invoke($this->onSubmit, $this);

if (!$handled) {
trigger_error('Form was submitted but there are no associated handlers.', E_USER_WARNING);
}
}


Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.CsrfProtection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $form = new Form;

$input = $form->addProtection('Security token did not match. Possible CSRF attack.');

$form->onSuccess[] = function () {};
$form->fireEvents();

Assert::same(['This field is required.'], $form->getErrors());
Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Controls.SelectBox.isOk.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ Form::initialize(true);
Validator::$messages[Nette\Forms\Controls\SelectBox::VALID] = 'SelectBox "%label" must be filled.';
$form = new Form;
$form->addSelect('foo', 'Foo', ['bar' => 'Bar']);
$form->onSuccess[] = function () {};
$form->fireEvents();
Assert::same(['SelectBox "Foo" must be filled.'], $form->getErrors());
1 change: 1 addition & 0 deletions tests/Forms/Forms.isValid.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ test('', function () {

$form->addError('1');
$form['item']->addError('2');
$form->onSuccess[] = function () {};
$form->fireEvents();

Assert::true($form->isSubmitted());
Expand Down
1 change: 1 addition & 0 deletions tests/Forms/Forms.renderer.1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ $defaults = [
];

$form->setDefaults($defaults);
$form->onSuccess[] = function () {};
$form->fireEvents();

Assert::matchFile(__DIR__ . '/Forms.renderer.1.expect', $form->__toString(true));
1 change: 1 addition & 0 deletions tests/Forms/Forms.renderer.2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ $defaults = [
];

$form->setDefaults($defaults);
$form->onSuccess[] = function () {};
$form->fireEvents();

Assert::matchFile(__DIR__ . '/Forms.renderer.2.expect', $form->__toString(true));
1 change: 1 addition & 0 deletions tests/Forms/Forms.renderer.3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $form = new Nette\Forms\Form;
$form->addHidden('userid');
$form->addSubmit('submit', 'Send');

$form->onSuccess[] = function () {};
$form->fireEvents();

Assert::match('<form action="" method="post">
Expand Down

0 comments on commit 842cb1e

Please sign in to comment.