Skip to content

Commit 7ee0f8c

Browse files
Milan Felix Šulcdg
authored andcommitted
Rules: added reset method (#180)
1 parent a2c892d commit 7ee0f8c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Forms/Rules.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ public function check(): bool
260260
}
261261

262262

263+
/**
264+
* Clear all validation rules.
265+
*/
266+
public function reset(): void
267+
{
268+
$this->rules = [];
269+
}
270+
271+
263272
/**
264273
* Validates single rule.
265274
*/

tests/Forms/Rules.reset.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Forms\Form;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
test(function () {
13+
$form = new Form;
14+
$input = $form->addText('text');
15+
$input->addCondition(false)
16+
->setRequired();
17+
18+
Assert::count(1, iterator_to_array($input->getRules()));
19+
$input->getRules()->reset();
20+
21+
Assert::count(0, iterator_to_array($input->getRules()));
22+
});
23+
24+
25+
test(function () {
26+
$form = new Form;
27+
$input1 = $form->addText('text1');
28+
$input2 = $form->addText('text2');
29+
$input2->addConditionOn($input1, $form::FILLED)
30+
->addRule($form::BLANK);
31+
32+
Assert::count(0, iterator_to_array($input1->getRules()));
33+
Assert::count(1, iterator_to_array($input2->getRules()));
34+
$input2->getRules()->reset();
35+
36+
Assert::count(0, iterator_to_array($input2->getRules()));
37+
});

0 commit comments

Comments
 (0)