Skip to content

Commit b6e395c

Browse files
committed
Form::$onSuccess and Container::$onValidate must be array of Traversable
1 parent 6588123 commit b6e395c

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/Forms/Container.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,15 @@ public function validate(array $controls = NULL)
134134
foreach ($controls === NULL ? $this->getComponents() : $controls as $control) {
135135
$control->validate();
136136
}
137-
foreach ($this->onValidate ?: [] as $handler) {
138-
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
139-
$values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
140-
Nette\Utils\Callback::invoke($handler, $this, $values);
137+
if ($this->onValidate !== NULL) {
138+
if (!is_array($this->onValidate) && !$this->onValidate instanceof \Traversable) {
139+
throw new Nette\UnexpectedValueException('Property Form::$onValidate must be array or Traversable, ' . gettype($this->onValidate) . ' given.');
140+
}
141+
foreach ($this->onValidate as $handler) {
142+
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
143+
$values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
144+
Nette\Utils\Callback::invoke($handler, $this, $values);
145+
}
141146
}
142147
$this->validated = TRUE;
143148
}

src/Forms/Form.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,11 @@ public function fireEvents()
400400

401401
if (!$this->isValid()) {
402402
$this->onError($this);
403-
} elseif ($this->onSuccess) {
403+
404+
} elseif ($this->onSuccess !== NULL) {
405+
if (!is_array($this->onSuccess) && !$this->onSuccess instanceof \Traversable) {
406+
throw new Nette\UnexpectedValueException('Property Form::$onSuccess must be array or Traversable, ' . gettype($this->onSuccess) . ' given.');
407+
}
404408
foreach ($this->onSuccess as $handler) {
405409
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
406410
$values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;

tests/Forms/Container.validate().phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ Assert::same([
3232
'fail 1',
3333
'fail 2',
3434
], $form->getErrors());
35+
36+
37+
Assert::exception(function () {
38+
$form = new Form;
39+
$form->onValidate = TRUE;
40+
$form->validate();
41+
}, Nette\UnexpectedValueException::class, 'Property Form::$onValidate must be array or Traversable, boolean given.');

tests/Forms/Forms.onSuccess.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,10 @@ $form->onError[] = function () use (& $called) {
5353
};
5454
$form->fireEvents();
5555
Assert::same([1, 2, 'err'], $called);
56+
57+
58+
Assert::exception(function () {
59+
$form = new Form;
60+
$form->onSuccess = TRUE;
61+
$form->fireEvents();
62+
}, Nette\UnexpectedValueException::class, 'Property Form::$onSuccess must be array or Traversable, boolean given.');

0 commit comments

Comments
 (0)