Skip to content

Commit 98b6a73

Browse files
committed
Rules: elseCondition for FILLED in BLANK and vice versa
1 parent d77b722 commit 98b6a73

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/Forms/Rules.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Rules implements \IteratorAggregate
1919
{
2020
use Nette\SmartObject;
2121

22+
private const NEG_RULES = [
23+
Form::FILLED => Form::BLANK,
24+
Form::BLANK => Form::FILLED,
25+
];
26+
2227
/** @var Rule|null */
2328
private $required;
2429

@@ -154,7 +159,11 @@ public function addConditionOn(IControl $control, $validator, $arg = null)
154159
public function elseCondition()
155160
{
156161
$rule = clone end($this->parent->rules);
157-
$rule->isNegative = !$rule->isNegative;
162+
if (isset(self::NEG_RULES[$rule->validator])) {
163+
$rule->validator = self::NEG_RULES[$rule->validator];
164+
} else {
165+
$rule->isNegative = !$rule->isNegative;
166+
}
158167
$rule->branch = new static($this->parent->control);
159168
$rule->branch->parent = $this->parent;
160169
$this->parent->rules[] = $rule;
@@ -293,10 +302,10 @@ private function adjustOperation(Rule $rule): void
293302
$name = strncmp($rule->validator, ':', 1) ? $rule->validator : 'Form:' . strtoupper($rule->validator);
294303
trigger_error("Negative validation rules such as ~$name are deprecated.", E_USER_DEPRECATED);
295304
}
296-
if ($rule->validator === Form::FILLED) {
297-
$rule->validator = Form::BLANK;
305+
if (isset(self::NEG_RULES[$rule->validator])) {
306+
$rule->validator = self::NEG_RULES[$rule->validator];
298307
$rule->isNegative = false;
299-
trigger_error('Replace negative validation rule ~Form::FILLED with Form::BLANK.', E_USER_DEPRECATED);
308+
trigger_error('Replace negative validation rule ~Form::FILLED with Form::BLANK and vice versa.', E_USER_DEPRECATED);
300309
}
301310
}
302311

tests/Forms/Controls.TextBase.loadData.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,20 @@ test(function () { // filter in BLANK condition
205205
$input->validate();
206206
Assert::same('default', $input->getValue());
207207
});
208+
209+
210+
test(function () { // filter in !FILLED condition
211+
$_POST = ['text' => ''];
212+
213+
$form = new Form;
214+
$input = $form->addText('text');
215+
$input->addCondition($form::FILLED)
216+
->elseCondition()
217+
->addFilter(function () use ($input) {
218+
return 'default';
219+
});
220+
221+
Assert::same('', $input->getValue());
222+
$input->validate();
223+
Assert::same('default', $input->getValue());
224+
});

0 commit comments

Comments
 (0)