Skip to content

Commit

Permalink
fixed validation after 1592cf6 [Closes #268]
Browse files Browse the repository at this point in the history
thx solcik@gmail.com
  • Loading branch information
dg committed Feb 10, 2021
1 parent c0389c9 commit f64c100
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Forms/Controls/TextBase.php
Expand Up @@ -139,7 +139,7 @@ public function addRule($validator, $errorMessage = null, $arg = null)
{
foreach ($this->getRules() as $rule) {
if (!$rule->canExport() && !$rule->branch) {
return $this;
return parent::addRule($validator, $errorMessage, $arg);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/TextInput.php
Expand Up @@ -70,7 +70,7 @@ public function addRule($validator, $errorMessage = null, $arg = null)
{
foreach ($this->getRules() as $rule) {
if (!$rule->canExport() && !$rule->branch) {
return $this;
return parent::addRule($validator, $errorMessage, $arg);
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Forms/Rules.valid.phpt
Expand Up @@ -42,6 +42,44 @@ test('', function () {
}, Nette\InvalidArgumentException::class, 'You cannot use Form::VALID in the addRule method.');
});

test('', function () {
$form = new Form;
$form->addText('foo')
->addFilter(function ($value) {
return str_replace(' ', '', $value);
})
->addRule($form::PATTERN, 'only numbers', '\d{5}');

$form['foo']->setValue('160 00');
$form->validate();
Assert::same([], $form->getErrors());

$form['foo']->setValue('160 00 x');
$form->validate();
Assert::same(['only numbers'], $form->getErrors());
});


test('', function () {
$form = new Form;
$foo = $form->addText('foo');
$rules = $foo->getRules();
$rules->addFilter(
function ($value) {
return str_replace(' ', '', $value);
}
);
$rules->addRule($form::PATTERN, 'only numbers', '\d{5}');

$form['foo']->setValue('160 00');
$form->validate();
Assert::same([], $form->getErrors());

$form['foo']->setValue('160 00 x');
$form->validate();
Assert::same(['only numbers'], $form->getErrors());
});


test('', function () {
Assert::exception(function () {
Expand Down

0 comments on commit f64c100

Please sign in to comment.