Skip to content

Commit 903a0b9

Browse files
castamirdg
authored andcommitted
Container: to the onValidate callbacks are passed values via second parameter (to be compatible with onSuccess handlers) [Closes #44]
1 parent 46875ea commit 903a0b9

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/Forms/Container.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ public function validate(array $controls = NULL)
139139
foreach ($controls === NULL ? $this->getComponents() : $controls as $control) {
140140
$control->validate();
141141
}
142-
$this->onValidate($this);
142+
foreach ($this->onValidate ?: array() as $handler) {
143+
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
144+
$values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
145+
Nette\Utils\Callback::invoke($handler, $this, $values);
146+
}
143147
$this->validated = TRUE;
144148
}
145149

tests/Forms/Forms.callbackParameters.phpt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Test: Nette\Forms success callback takes $form and $values parameters.
4+
* Test: Nette\Forms success and validate callback takes $form and $values parameters.
55
*/
66

77
use Nette\Utils\ArrayHash;
@@ -67,16 +67,19 @@ $f3 = function ($form, $values) use (& $types) {
6767
$f4 = function ($form, $values) use (& $types) {
6868
$values->text = 'b';
6969
};
70-
$arrayHashIsImmutable = FALSE;
70+
$arrayHashIsImmutable = array();
7171
$f5 = function ($form, $values) use (& $arrayHashIsImmutable) {
72-
$arrayHashIsImmutable = $values->text === 'a';
72+
$arrayHashIsImmutable[] = $values->text === 'a';
7373
};
7474

7575
foreach (array($m1, $m2, $m3, $f1, $f2, $f3, $f4, $f5) as $f) {
7676
$form->onSuccess[] = $f;
7777
}
78+
foreach (array($m1, $m2, $m3, $f1, $f2, $f3, $f4, $f5) as $f) {
79+
$form->onValidate[] = $f;
80+
}
7881
$form->fireEvents();
7982

80-
Assert::same(TestFormCallbackParameters::$results, array(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE));
81-
Assert::same($types, array(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE));
82-
Assert::true($arrayHashIsImmutable);
83+
Assert::same(TestFormCallbackParameters::$results, array(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE));
84+
Assert::same($types, array(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE));
85+
Assert::same($arrayHashIsImmutable, array(TRUE, TRUE));

0 commit comments

Comments
 (0)