Skip to content

Commit 081f2ba

Browse files
committed
events refactoring, added default values, removed magic
1 parent 1ec243b commit 081f2ba

File tree

7 files changed

+26
-62
lines changed

7 files changed

+26
-62
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"php": ">=7.2 <8.1",
1919
"nette/component-model": "^3.0",
2020
"nette/http": "^3.1",
21-
"nette/utils": "^3.2"
21+
"nette/utils": "^3.2.1"
2222
},
2323
"require-dev": {
2424
"nette/application": "^3.0",

src/Forms/Container.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Container extends Nette\ComponentModel\Container implements \ArrayAccess
2727
private const ARRAY = 'array';
2828

2929
/** @var callable[]&(callable(Container, mixed): void)[]; Occurs when the form is validated */
30-
public $onValidate;
30+
public $onValidate = [];
3131

3232
/** @var ControlGroup|null */
3333
protected $currentGroup;
@@ -188,19 +188,13 @@ public function validate(array $controls = null): void
188188
}
189189
$this->validated = true;
190190

191-
if ($this->onValidate !== null) {
192-
if (!is_iterable($this->onValidate)) {
193-
throw new Nette\UnexpectedValueException('Property Form::$onValidate must be iterable, ' . gettype($this->onValidate) . ' given.');
194-
}
195-
196-
$isValid = !$this->getErrors();
197-
foreach ($this->onValidate as $handler) {
198-
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
199-
$values = isset($params[1]) && $isValid
200-
? $this->getValues($params[1]->getType() instanceof \ReflectionNamedType ? $params[1]->getType()->getName() : null)
201-
: null;
202-
$handler($this, $values);
203-
}
191+
$isValid = !$this->getErrors();
192+
foreach ($this->onValidate as $handler) {
193+
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
194+
$values = isset($params[1]) && $isValid
195+
? $this->getValues($params[1]->getType() instanceof \ReflectionNamedType ? $params[1]->getType()->getName() : null)
196+
: null;
197+
$handler($this, $values);
204198
}
205199
}
206200

src/Forms/Controls/SubmitButton.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
class SubmitButton extends Button implements Nette\Forms\SubmitterControl
2121
{
2222
/** @var callable[]&(callable(SubmitButton): void)[]; Occurs when the button is clicked and form is successfully validated */
23-
public $onClick;
23+
public $onClick = [];
2424

2525
/** @var callable[]&(callable(SubmitButton): void)[]; Occurs when the button is clicked and form is not validated */
26-
public $onInvalidClick;
26+
public $onInvalidClick = [];
2727

2828
/** @var array|null */
2929
private $validationScope;
@@ -93,7 +93,7 @@ public function getValidationScope(): ?array
9393
*/
9494
public function click(): void
9595
{
96-
$this->onClick($this);
96+
Nette\Utils\Arrays::invoke($this->onClick, $this);
9797
}
9898

9999

src/Forms/Form.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Forms;
1111

1212
use Nette;
13+
use Nette\Utils\Arrays;
1314
use Nette\Utils\Html;
1415

1516

@@ -82,16 +83,16 @@ class Form extends Container implements Nette\HtmlStringable
8283
public const PROTECTOR_ID = '_token_';
8384

8485
/** @var callable[]&(callable(Form, mixed): void)[]; Occurs when the form is submitted and successfully validated */
85-
public $onSuccess;
86+
public $onSuccess = [];
8687

8788
/** @var callable[]&(callable(Form): void)[]; Occurs when the form is submitted and is not valid */
88-
public $onError;
89+
public $onError = [];
8990

9091
/** @var callable[]&(callable(Form): void)[]; Occurs when the form is submitted */
91-
public $onSubmit;
92+
public $onSubmit = [];
9293

9394
/** @var callable[]&(callable(Form): void)[]; Occurs before the form is rendered */
94-
public $onRender;
95+
public $onRender = [];
9596

9697
/** @internal @var Nette\Http\IRequest used only by standalone form */
9798
public $httpRequest;
@@ -420,31 +421,21 @@ public function fireEvents(): void
420421

421422
if ($this->submittedBy instanceof SubmitterControl) {
422423
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);
429425
} else {
430-
$this->submittedBy->onInvalidClick($this->submittedBy);
426+
Arrays::invoke($this->submittedBy->onInvalidClick, $this->submittedBy);
431427
}
432428
}
433429

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()) {
441431
$this->invokeHandlers($this->onSuccess);
442-
if (!$this->isValid()) {
443-
$this->onError($this);
444-
}
445432
}
446433

447-
$this->onSubmit($this);
434+
if (!$this->isValid()) {
435+
Arrays::invoke($this->onError, $this);
436+
}
437+
438+
Arrays::invoke($this->onSubmit, $this);
448439
}
449440

450441

@@ -630,7 +621,7 @@ public function fireRenderEvents(): void
630621
if (!$this->beforeRenderCalled) {
631622
$this->beforeRenderCalled = true;
632623
$this->beforeRender();
633-
$this->onRender($this);
624+
Arrays::invoke($this->onRender, $this);
634625
}
635626
}
636627

tests/Forms/Container.validate().phpt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,3 @@ Assert::same([
3434
'fail 1',
3535
'fail 2',
3636
], $form->getErrors());
37-
38-
39-
Assert::exception(function () {
40-
$form = new Form;
41-
$form->onValidate = true;
42-
$form->validate();
43-
}, Nette\UnexpectedValueException::class, 'Property Form::$onValidate must be iterable, boolean given.');

tests/Forms/Forms.onClick.phpt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,3 @@ test('invalid', function () {
106106
$form->fireEvents();
107107
Assert::same(['invalidClick', 'error', 'submit'], $called);
108108
});
109-
110-
111-
Assert::exception(function () {
112-
$form = new Form;
113-
$form->addSubmit('btn')->onClick = true;
114-
$form->fireEvents();
115-
}, Nette\UnexpectedValueException::class, "Property \$onClick in button 'btn' must be iterable, boolean given.");

tests/Forms/Forms.onSuccess.phpt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,3 @@ test('invalid', function () {
8888
$form->fireEvents();
8989
Assert::same(['error', 'submit'], $called);
9090
});
91-
92-
93-
Assert::exception(function () {
94-
$form = new Form;
95-
$form->onSuccess = true;
96-
$form->fireEvents();
97-
}, Nette\UnexpectedValueException::class, 'Property Form::$onSuccess must be iterable, boolean given.');

0 commit comments

Comments
 (0)