Skip to content

Commit eb23dcd

Browse files
matej21dg
authored andcommitted
Rules: allow Form::VALID only in the addConditionOn [Closes #95][Closes #101]
1 parent ac902b1 commit eb23dcd

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Forms/Rules.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public function isRequired()
7575
*/
7676
public function addRule($validator, $message = NULL, $arg = NULL)
7777
{
78+
if ($validator === Form::VALID || $validator === ~Form::VALID) {
79+
throw new Nette\InvalidArgumentException('You cannot use Form::VALID in the addRule method.');
80+
}
7881
$rule = new Rule;
7982
$rule->control = $this->control;
8083
$rule->validator = $validator;
@@ -98,6 +101,9 @@ public function addRule($validator, $message = NULL, $arg = NULL)
98101
*/
99102
public function addCondition($validator, $arg = NULL)
100103
{
104+
if ($validator === Form::VALID || $validator === ~Form::VALID) {
105+
throw new Nette\InvalidArgumentException('You cannot use Form::VALID in the addCondition method.');
106+
}
101107
return $this->addConditionOn($this->control, $validator, $arg);
102108
}
103109

tests/Forms/Rules.valid.phpt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Forms\Rules.
5+
*/
6+
7+
use Nette\Forms\Form;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
test(function () {
15+
$form = new Form;
16+
$form->addText('foo')
17+
->setRequired('fill foo');
18+
$form->addText('bar')
19+
->addConditionOn($form['foo'], Form::VALID)
20+
->setRequired('fill bar');
21+
22+
$form->validate();
23+
Assert::same(['fill foo'], $form->getErrors());
24+
25+
$form['foo']->setValue('abc');
26+
$form->validate();
27+
Assert::same(['fill bar'], $form->getErrors());
28+
29+
$form['bar']->setValue('abc');
30+
$form->validate();
31+
Assert::same([], $form->getErrors());
32+
});
33+
34+
35+
test(function () {
36+
Assert::exception(function () {
37+
$form = new Form;
38+
$form->addText('foo')
39+
->addRule(Form::VALID);
40+
}, Nette\InvalidArgumentException::class, 'You cannot use Form::VALID in the addRule method.');
41+
});
42+
43+
44+
test(function () {
45+
Assert::exception(function () {
46+
$form = new Form;
47+
$form->addText('foo')
48+
->addCondition(Form::VALID);
49+
}, Nette\InvalidArgumentException::class, 'You cannot use Form::VALID in the addCondition method.');
50+
});
51+
52+
53+
test(function () {
54+
Assert::exception(function () {
55+
$form = new Form;
56+
$form->addText('foo')
57+
->addRule(~Form::VALID);
58+
}, Nette\InvalidArgumentException::class, 'You cannot use Form::VALID in the addRule method.');
59+
});

0 commit comments

Comments
 (0)