Skip to content

Commit f64c100

Browse files
committed
fixed validation after 1592cf6 [Closes #268]
thx [email protected]
1 parent c0389c9 commit f64c100

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/Forms/Controls/TextBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function addRule($validator, $errorMessage = null, $arg = null)
139139
{
140140
foreach ($this->getRules() as $rule) {
141141
if (!$rule->canExport() && !$rule->branch) {
142-
return $this;
142+
return parent::addRule($validator, $errorMessage, $arg);
143143
}
144144
}
145145

src/Forms/Controls/TextInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function addRule($validator, $errorMessage = null, $arg = null)
7070
{
7171
foreach ($this->getRules() as $rule) {
7272
if (!$rule->canExport() && !$rule->branch) {
73-
return $this;
73+
return parent::addRule($validator, $errorMessage, $arg);
7474
}
7575
}
7676

tests/Forms/Rules.valid.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,44 @@ test('', function () {
4242
}, Nette\InvalidArgumentException::class, 'You cannot use Form::VALID in the addRule method.');
4343
});
4444

45+
test('', function () {
46+
$form = new Form;
47+
$form->addText('foo')
48+
->addFilter(function ($value) {
49+
return str_replace(' ', '', $value);
50+
})
51+
->addRule($form::PATTERN, 'only numbers', '\d{5}');
52+
53+
$form['foo']->setValue('160 00');
54+
$form->validate();
55+
Assert::same([], $form->getErrors());
56+
57+
$form['foo']->setValue('160 00 x');
58+
$form->validate();
59+
Assert::same(['only numbers'], $form->getErrors());
60+
});
61+
62+
63+
test('', function () {
64+
$form = new Form;
65+
$foo = $form->addText('foo');
66+
$rules = $foo->getRules();
67+
$rules->addFilter(
68+
function ($value) {
69+
return str_replace(' ', '', $value);
70+
}
71+
);
72+
$rules->addRule($form::PATTERN, 'only numbers', '\d{5}');
73+
74+
$form['foo']->setValue('160 00');
75+
$form->validate();
76+
Assert::same([], $form->getErrors());
77+
78+
$form['foo']->setValue('160 00 x');
79+
$form->validate();
80+
Assert::same(['only numbers'], $form->getErrors());
81+
});
82+
4583

4684
test('', function () {
4785
Assert::exception(function () {

0 commit comments

Comments
 (0)