|
10 | 10 | namespace Nette\Forms; |
11 | 11 |
|
12 | 12 | use Nette; |
| 13 | +use Nette\Utils\Arrays; |
13 | 14 | use Nette\Utils\Html; |
14 | 15 |
|
15 | 16 |
|
@@ -82,16 +83,16 @@ class Form extends Container implements Nette\HtmlStringable |
82 | 83 | public const PROTECTOR_ID = '_token_'; |
83 | 84 |
|
84 | 85 | /** @var callable[]&(callable(Form, mixed): void)[]; Occurs when the form is submitted and successfully validated */ |
85 | | - public $onSuccess; |
| 86 | + public $onSuccess = []; |
86 | 87 |
|
87 | 88 | /** @var callable[]&(callable(Form): void)[]; Occurs when the form is submitted and is not valid */ |
88 | | - public $onError; |
| 89 | + public $onError = []; |
89 | 90 |
|
90 | 91 | /** @var callable[]&(callable(Form): void)[]; Occurs when the form is submitted */ |
91 | | - public $onSubmit; |
| 92 | + public $onSubmit = []; |
92 | 93 |
|
93 | 94 | /** @var callable[]&(callable(Form): void)[]; Occurs before the form is rendered */ |
94 | | - public $onRender; |
| 95 | + public $onRender = []; |
95 | 96 |
|
96 | 97 | /** @internal @var Nette\Http\IRequest used only by standalone form */ |
97 | 98 | public $httpRequest; |
@@ -420,31 +421,21 @@ public function fireEvents(): void |
420 | 421 |
|
421 | 422 | if ($this->submittedBy instanceof SubmitterControl) { |
422 | 423 | if ($this->isValid()) { |
423 | | - if ($handlers = $this->submittedBy->onClick) { |
424 | | - if (!is_iterable($handlers)) { |
425 | | - throw new Nette\UnexpectedValueException("Property \$onClick in button '{$this->submittedBy->getName()}' must be iterable, " . gettype($handlers) . ' given.'); |
426 | | - } |
427 | | - $this->invokeHandlers($handlers, $this->submittedBy); |
428 | | - } |
| 424 | + $this->invokeHandlers($this->submittedBy->onClick, $this->submittedBy); |
429 | 425 | } else { |
430 | | - $this->submittedBy->onInvalidClick($this->submittedBy); |
| 426 | + Arrays::invoke($this->submittedBy->onInvalidClick, $this->submittedBy); |
431 | 427 | } |
432 | 428 | } |
433 | 429 |
|
434 | | - if (!$this->isValid()) { |
435 | | - $this->onError($this); |
436 | | - |
437 | | - } elseif ($this->onSuccess !== null) { |
438 | | - if (!is_iterable($this->onSuccess)) { |
439 | | - throw new Nette\UnexpectedValueException('Property Form::$onSuccess must be iterable, ' . gettype($this->onSuccess) . ' given.'); |
440 | | - } |
| 430 | + if ($this->isValid()) { |
441 | 431 | $this->invokeHandlers($this->onSuccess); |
442 | | - if (!$this->isValid()) { |
443 | | - $this->onError($this); |
444 | | - } |
445 | 432 | } |
446 | 433 |
|
447 | | - $this->onSubmit($this); |
| 434 | + if (!$this->isValid()) { |
| 435 | + Arrays::invoke($this->onError, $this); |
| 436 | + } |
| 437 | + |
| 438 | + Arrays::invoke($this->onSubmit, $this); |
448 | 439 | } |
449 | 440 |
|
450 | 441 |
|
@@ -630,7 +621,7 @@ public function fireRenderEvents(): void |
630 | 621 | if (!$this->beforeRenderCalled) { |
631 | 622 | $this->beforeRenderCalled = true; |
632 | 623 | $this->beforeRender(); |
633 | | - $this->onRender($this); |
| 624 | + Arrays::invoke($this->onRender, $this); |
634 | 625 | } |
635 | 626 | } |
636 | 627 |
|
|
0 commit comments