Skip to content

Commit 4e83559

Browse files
committed
$onClick handlers are called with argument $values (as $onSuccess)
1 parent 6a29dac commit 4e83559

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/Forms/Form.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,12 @@ public function fireEvents(): void
400400

401401
if ($this->submittedBy instanceof ISubmitterControl) {
402402
if ($this->isValid()) {
403-
$this->submittedBy->onClick($this->submittedBy);
403+
if ($handlers = $this->submittedBy->onClick) {
404+
if (!is_iterable($handlers)) {
405+
throw new Nette\UnexpectedValueException("Property \$onClick in button '{$this->submittedBy->getName()}' must be iterable, " . gettype($handlers) . ' given.');
406+
}
407+
$this->invokeHandlers($handlers, $this->submittedBy);
408+
}
404409
} else {
405410
$this->submittedBy->onInvalidClick($this->submittedBy);
406411
}
@@ -413,21 +418,29 @@ public function fireEvents(): void
413418
if (!is_iterable($this->onSuccess)) {
414419
throw new Nette\UnexpectedValueException('Property Form::$onSuccess must be iterable, ' . gettype($this->onSuccess) . ' given.');
415420
}
416-
foreach ($this->onSuccess as $handler) {
417-
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
418-
$values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
419-
Nette\Utils\Callback::invoke($handler, $this, $values);
420-
if (!$this->isValid()) {
421-
$this->onError($this);
422-
break;
423-
}
421+
$this->invokeHandlers($this->onSuccess);
422+
if (!$this->isValid()) {
423+
$this->onError($this);
424424
}
425425
}
426426

427427
$this->onSubmit($this);
428428
}
429429

430430

431+
private function invokeHandlers(iterable $handlers, $button = NULL): void
432+
{
433+
foreach ($handlers as $handler) {
434+
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
435+
$values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
436+
Nette\Utils\Callback::invoke($handler, $button ?: $this, $values);
437+
if (!$button && !$this->isValid()) {
438+
return;
439+
}
440+
}
441+
}
442+
443+
431444
/**
432445
* Internal: returns submitted HTTP data or NULL when form was not submitted.
433446
*/

tests/Forms/Forms.callbackParameters.phpt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ declare(strict_types=1);
88

99
use Nette\Utils\ArrayHash;
1010
use Nette\Forms\Form;
11+
use Nette\Forms\Controls\SubmitButton;
1112
use Tester\Assert;
1213

1314
require __DIR__ . '/../bootstrap.php';
1415

1516
$_SERVER['REQUEST_METHOD'] = 'POST';
1617
$_POST['text'] = 'a';
18+
$_POST['btn'] = 'b';
1719

1820
$form = new Form();
1921
$form->addText('text');
20-
$form->addSubmit('submit');
22+
$form->addSubmit('btn');
2123

2224
$types = [];
2325

@@ -33,6 +35,18 @@ $f2 = function ($form, array $values) use (&$types) {
3335
$types[] = is_array($values);
3436
};
3537

38+
// Test the closure second parameter to be an ArrayHash instance by default
39+
$b1 = function ($form, $values) use (&$types) {
40+
$types[] = $form instanceof SubmitButton;
41+
$types[] = $values instanceof ArrayHash;
42+
};
43+
44+
// Test the closure second parameter to be an array by type-hint
45+
$b2 = function ($form, array $values) use (&$types) {
46+
$types[] = $form instanceof SubmitButton;
47+
$types[] = is_array($values);
48+
};
49+
3650
// Test the second parameter in ArrayHash form to be immutable
3751
$f4 = function ($form, $values) use (&$types) {
3852
$values->text = 'b';
@@ -44,7 +58,8 @@ $f5 = function ($form, $values) use (&$arrayHashIsImmutable) {
4458

4559
$form->onSuccess = [$f1, $f2, $f1, $f4, $f5];
4660
$form->onValidate = [$f1, $f2, $f1, $f4, $f5];
61+
$form['btn']->onClick = [$b1, $b2, $b1, $f4, $f5];
4762
$form->fireEvents();
4863

49-
Assert::same([TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE], $types);
50-
Assert::same([TRUE, TRUE], $arrayHashIsImmutable);
64+
Assert::same([TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE], $types);
65+
Assert::same([TRUE, TRUE, TRUE], $arrayHashIsImmutable);

tests/Forms/Forms.onClick.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ Assert::exception(function () {
7373
$form = new Form;
7474
$form->addSubmit('btn')->onClick = TRUE;
7575
$form->fireEvents();
76-
}, Nette\UnexpectedValueException::class, 'Property Nette\Forms\Controls\SubmitButton::$onClick must be array or NULL, boolean given.');
76+
}, Nette\UnexpectedValueException::class, "Property \$onClick in button 'btn' must be iterable, boolean given.");

0 commit comments

Comments
 (0)