Skip to content

Commit 9650ade

Browse files
committed
Revert "Form::onValidate - values are passed only when form is valid"
This reverts commit b67ff3f.
1 parent 5c92a63 commit 9650ade

2 files changed

Lines changed: 12 additions & 23 deletions

File tree

src/Forms/Container.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,13 @@ public function validate(array $controls = null): void
188188
}
189189
$this->validated = true;
190190

191-
$isValid = !$this->getErrors();
192191
foreach ($this->onValidate as $handler) {
193192
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
194193
$types = array_map([Nette\Utils\Reflection::class, 'getParameterType'], $params);
195-
$handler(
196-
!isset($types[0]) || $this instanceof $types[0]
197-
? $this
198-
: ($isValid ? $this->getValues($types[0]) : null),
199-
isset($params[1]) && $isValid
200-
? $this->getValues($types[1])
201-
: null
202-
);
194+
$args = isset($types[0]) && !$this instanceof $types[0]
195+
? [$this->getValues($types[0])]
196+
: [$this, isset($params[1]) ? $this->getValues($types[1]) : null];
197+
$handler(...$args);
203198
}
204199
}
205200

tests/Forms/Forms.validationScope.phpt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,29 @@ require __DIR__ . '/../bootstrap.php';
1515

1616
//Tracy\Debugger::enable();
1717
$datasets = [
18-
['send1', ['container', 'form', 'name', 'age', 'age2'], null],
19-
['send2', ['form'], ['optional' => '', 'details' => []]],
20-
['send3', ['form', 'name'], null],
21-
['send4', ['form', 'age'], null],
22-
['send5', ['container', 'form', 'age', 'age2'], null],
18+
['send1', ['container', 'form', 'name', 'age', 'age2']],
19+
['send2', ['form']],
20+
['send3', ['form', 'name']],
21+
['send4', ['form', 'age']],
22+
['send5', ['container', 'form', 'age', 'age2']],
2323
];
2424

2525
foreach ($datasets as $case) {
2626
$form = new Form;
27-
$form->onValidate[] = function (Form $form, ?array $values) use (&$values1) {
27+
$form->onValidate[] = function (Form $form) {
2828
$form->addError('form');
29-
$values1 = $values;
3029
};
3130
$form->addText('name')->setRequired('name');
32-
$form->addText('optional');
3331

3432
$details = $form->addContainer('details');
35-
$details->onValidate[] = function (Container $container, $values) use (&$values2) {
33+
$details->onValidate[] = function (Container $container) {
3634
$container->getForm()->addError('container');
37-
$values2 = $values;
3835
};
3936
$details->addText('age')->setRequired('age');
4037
$details->addText('age2')->setRequired('age2');
4138

4239
$form->addSubmit('send1');
43-
$form->addSubmit('send2')->setValidationScope([$form['optional']]);
40+
$form->addSubmit('send2')->setValidationScope([]);
4441
$form->addSubmit('send3')->setValidationScope([$form['name']]);
4542
$form->addSubmit('send4')->setValidationScope([$form['details']['age']]);
4643
$form->addSubmit('send5')->setValidationScope([$form['details']]);
@@ -50,7 +47,4 @@ foreach ($datasets as $case) {
5047
Assert::truthy($form->isSubmitted());
5148
$form->validate();
5249
Assert::equal($case[1], $form->getErrors());
53-
54-
Assert::same($case[2], $values1);
55-
Assert::null($values2);
5650
}

0 commit comments

Comments
 (0)