Skip to content

Commit 000677f

Browse files
committed
Forms: negative rules like ~$form::ABCD are deprecated. (Negatives for FILLED & EQUAL are BLANK & NOT_EQUAL)
Negative rules are used occasionally, but makes a lot of complications, especially with default error messages. For cases where is negative validator needed, it is better to create new validator
1 parent d1c80c4 commit 000677f

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

src/Forms/Rules.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ public function addConditionOn(IControl $control, $validator, $arg = NULL)
122122
$rule = new Rule;
123123
$rule->control = $control;
124124
$rule->validator = $validator;
125-
$this->adjustOperation($rule);
126125
$rule->arg = $arg;
127126
$rule->branch = new static($this->control);
128127
$rule->branch->parent = $this;
128+
$this->adjustOperation($rule);
129129

130130
$this->rules[] = $rule;
131131
return $rule->branch;
@@ -277,6 +277,10 @@ private function adjustOperation($rule)
277277
if (is_string($rule->validator) && ord($rule->validator[0]) > 127) {
278278
$rule->isNegative = TRUE;
279279
$rule->validator = ~$rule->validator;
280+
if (!$rule->branch) {
281+
$name = strncmp($rule->validator, ':', 1) ? $rule->validator : 'Form:' . strtoupper($rule->validator);
282+
trigger_error("Negative validation rules such as ~$name are deprecated.", E_USER_DEPRECATED);
283+
}
280284
}
281285

282286
if (!is_callable($this->getCallback($rule))) {

tests/Forms/Controls.TextBase.loadData.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ test(function () { // non float
133133
$_POST = ['number' => ' 10,5 '];
134134

135135
$form = new Form;
136-
$input = $form->addText('number')
137-
->addRule(~$form::FLOAT);
136+
$input = @$form->addText('number')
137+
->addRule(~$form::FLOAT); // @ - negative rules are deprecated
138138

139139
$input->validate();
140140
Assert::same(10.5, $input->getValue()); // side effect

tests/Forms/Forms.userValidator.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ function myValidator1($item, $arg)
2626
foreach ($datasets as $case) {
2727

2828
$form = new Form;
29-
$control = $form->addText('value', 'Value:')
29+
$control = @$form->addText('value', 'Value:')
3030
->addRule('myValidator1', 'Value %d is not allowed!', 11)
31-
->addRule(~'myValidator1', 'Value %d is required!', 22);
31+
->addRule(~'myValidator1', 'Value %d is required!', 22); // @ - negative rules are deprecated
3232

3333
$control->setValue($case[0])->validate();
3434
Assert::same($case[1], $control->getErrors());

tests/Forms/Rules.required.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ test(function () { // 'required' is always the first rule
5555
Assert::same(Form::REQUIRED, $items[0]->validator);
5656
Assert::same(Form::EMAIL, $items[1]->validator);
5757

58-
$rules->addRule(~$form::REQUIRED);
58+
@$rules->addRule(~$form::REQUIRED); // @ - negative rules are deprecated
5959
$items = iterator_to_array($rules);
6060
Assert::count(2, $items);
6161
Assert::same(Form::REQUIRED, $items[0]->validator);
@@ -90,6 +90,6 @@ test(function () { // addRule(~Form::REQUIRED)
9090
$input = $form->addText('text');
9191

9292
Assert::false($input->isRequired());
93-
Assert::same($input, $input->addRule(~Form::REQUIRED));
93+
Assert::same($input, @$input->addRule(~Form::REQUIRED)); // @ - negative rules are deprecated
9494
Assert::false($input->isRequired());
9595
});

tests/Forms/Rules.valid.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test(function () {
5353
test(function () {
5454
Assert::exception(function () {
5555
$form = new Form;
56-
$form->addText('foo')
57-
->addRule(~Form::VALID);
56+
@$form->addText('foo')
57+
->addRule(~Form::VALID); // @ - negative rules are deprecated
5858
}, Nette\InvalidArgumentException::class, 'You cannot use Form::VALID in the addRule method.');
5959
});

0 commit comments

Comments
 (0)