Skip to content

Commit 1ac626d

Browse files
committed
added support for static addCondition(TRUE | FALSE)
1 parent 1026a80 commit 1ac626d

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

src/Forms/Rules.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public function addCondition($validator, $arg = NULL)
109109
{
110110
if ($validator === Form::VALID || $validator === ~Form::VALID) {
111111
throw new Nette\InvalidArgumentException('You cannot use Form::VALID in the addCondition method.');
112+
} elseif (is_bool($validator)) {
113+
$arg = $validator;
114+
$validator = ':static';
112115
}
113116
return $this->addConditionOn($this->control, $validator, $arg);
114117
}

src/Forms/Validator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ public static function validateNotEqual(IControl $control, $arg): bool
111111
}
112112

113113

114+
/**
115+
* Returns argument.
116+
*/
117+
public static function validateStatic(IControl $control, bool $arg): bool
118+
{
119+
return $arg;
120+
}
121+
122+
114123
/**
115124
* Is control filled?
116125
*/

src/assets/netteForms.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,10 @@
531531
}
532532
}
533533
return true;
534+
},
535+
536+
'static': function (elem, arg, val) {
537+
return arg;
534538
}
535539
};
536540

tests/Forms/Rules.static.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Forms\Form;
6+
use Nette\Forms\Rules;
7+
use Tester\Assert;
8+
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
test(function () {
14+
$form = new Form;
15+
$input = $form->addText('text');
16+
$input->addCondition(FALSE)
17+
->setRequired();
18+
19+
$rules = $input->getRules();
20+
$items = iterator_to_array($rules);
21+
Assert::count(1, $items);
22+
Assert::same(':static', $items[0]->validator);
23+
Assert::false($items[0]->arg);
24+
Assert::type(Rules::class, $items[0]->branch);
25+
26+
Assert::true($rules->validate());
27+
});
28+
29+
30+
test(function () {
31+
$form = new Form;
32+
$input = $form->addText('text');
33+
$input->addCondition(TRUE)
34+
->setRequired();
35+
36+
$rules = $input->getRules();
37+
$items = iterator_to_array($rules);
38+
Assert::count(1, $items);
39+
Assert::same(':static', $items[0]->validator);
40+
Assert::true($items[0]->arg);
41+
Assert::type(Rules::class, $items[0]->branch);
42+
43+
Assert::false($rules->validate());
44+
Assert::same(['This field is required.'], $input->getErrors());
45+
});

tests/netteForms/spec/Nette.validateRuleSpec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ describe('Nette.getValue & validateRule', function() {
1616
expect(Nette.validateRule(el, 'filled')).toBe(false);
1717
expect(Nette.validateRule(el, 'blank')).toBe(true);
1818
expect(Nette.validateRule(el, 'equal', '')).toBe(true);
19+
expect(Nette.validateRule(el, 'static', true)).toBe(true);
20+
expect(Nette.validateRule(el, 'static', false)).toBe(false);
1921

2022
el.value = ' hello ';
2123
expect(Nette.getValue(el)).toBe('hello');

tests/netteForms/spec/Nette.validatorsSpec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ describe('Nette.validators', function() {
6868
expect(Nette.validators.url(null, null, 'http://_e_.example.com', v)).toBe(true);
6969
});
7070

71+
72+
it('static', function() {
73+
expect(Nette.validators.static(null, true)).toBe(true);
74+
expect(Nette.validators.static(null, false)).toBe(false);
75+
});
7176
});

0 commit comments

Comments
 (0)