Skip to content

Commit 84f2559

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

File tree

2 files changed

+44
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)