Skip to content

Commit ffaeebe

Browse files
committed
TextInput: rules are processed in addRule() instead of getControl() (is consistent with TextBase)
1 parent 7ff7d04 commit ffaeebe

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

src/Forms/Controls/TextInput.php

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,38 +58,6 @@ public function setType($type)
5858
public function getControl()
5959
{
6060
$input = parent::getControl();
61-
62-
foreach ($this->getRules() as $rule) {
63-
if ($rule->isNegative || $rule->branch) {
64-
65-
} elseif ($input->type === NULL && in_array($rule->validator, [Form::EMAIL, Form::URL, Form::INTEGER], TRUE)) {
66-
static $types = [Form::EMAIL => 'email', Form::URL => 'url', Form::INTEGER => 'number'];
67-
$input->type = $types[$rule->validator];
68-
69-
} elseif (in_array($rule->validator, [Form::MIN, Form::MAX, Form::RANGE], TRUE)
70-
&& in_array($input->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], TRUE)
71-
) {
72-
if ($rule->validator === Form::MIN) {
73-
$range = [$rule->arg, NULL];
74-
} elseif ($rule->validator === Form::MAX) {
75-
$range = [NULL, $rule->arg];
76-
} else {
77-
$range = $rule->arg;
78-
}
79-
if (isset($range[0]) && is_scalar($range[0])) {
80-
$input->min = isset($input->min) ? max($input->min, $range[0]) : $range[0];
81-
}
82-
if (isset($range[1]) && is_scalar($range[1])) {
83-
$input->max = isset($input->max) ? min($input->max, $range[1]) : $range[1];
84-
}
85-
86-
} elseif ($rule->validator === Form::PATTERN && is_scalar($rule->arg)
87-
&& in_array($input->type, [NULL, 'text', 'search', 'tel', 'url', 'email', 'password'], TRUE)
88-
) {
89-
$input->pattern = $rule->arg;
90-
}
91-
}
92-
9361
if ($input->type !== 'password' && ($this->rawValue !== '' || $this->emptyValue !== '')) {
9462
$input->value = $this->rawValue === ''
9563
? $this->translate($this->emptyValue)
@@ -99,4 +67,38 @@ public function getControl()
9967
return $input;
10068
}
10169

70+
71+
public function addRule($validator, $message = NULL, $arg = NULL)
72+
{
73+
$this->control = $this->control;
74+
if ($this->control->type === NULL && in_array($validator, [Form::EMAIL, Form::URL, Form::INTEGER], TRUE)) {
75+
static $types = [Form::EMAIL => 'email', Form::URL => 'url', Form::INTEGER => 'number'];
76+
$this->control->type = $types[$validator];
77+
78+
} elseif (in_array($validator, [Form::MIN, Form::MAX, Form::RANGE], TRUE)
79+
&& in_array($this->control->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], TRUE)
80+
) {
81+
if ($validator === Form::MIN) {
82+
$range = [$arg, NULL];
83+
} elseif ($validator === Form::MAX) {
84+
$range = [NULL, $arg];
85+
} else {
86+
$range = $arg;
87+
}
88+
if (isset($range[0]) && is_scalar($range[0])) {
89+
$this->control->min = isset($this->control->min) ? max($this->control->min, $range[0]) : $range[0];
90+
}
91+
if (isset($range[1]) && is_scalar($range[1])) {
92+
$this->control->max = isset($this->control->max) ? min($this->control->max, $range[1]) : $range[1];
93+
}
94+
95+
} elseif ($validator === Form::PATTERN && is_scalar($arg)
96+
&& in_array($this->control->type, [NULL, 'text', 'search', 'tel', 'url', 'email', 'password'], TRUE)
97+
) {
98+
$this->control->pattern = $arg;
99+
}
100+
101+
return parent::addRule($validator, $message, $arg);
102+
}
103+
102104
}

tests/Forms/Controls.TextInput.render.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ test(function () { // validation rule required & PATTERN
7777

7878
foreach (['text', 'search', 'tel', 'url', 'email'] as $type) {
7979
$input->setType($type);
80-
Assert::same('<input type="' . $type . '" name="text" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\' pattern="[0-9]+">', (string) $input->getControl());
80+
Assert::same('<input type="' . $type . '" name="text" pattern="[0-9]+" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\'>', (string) $input->getControl());
8181
}
8282
$input->setType('password');
83-
Assert::same('<input type="password" name="text" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\' pattern="[0-9]+">', (string) $input->getControl());
83+
Assert::same('<input type="password" name="text" pattern="[0-9]+" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\'>', (string) $input->getControl());
8484

8585
$input->setType('number');
86-
Assert::same('<input type="number" name="text" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\'>', (string) $input->getControl());
86+
Assert::same('<input type="number" name="text" pattern="[0-9]+" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"required"},{"op":":pattern","msg":"error message","arg":"[0-9]+"}]\'>', (string) $input->getControl());
8787
});
8888

8989

@@ -150,7 +150,7 @@ test(function () { // validation rule RANGE with setType
150150
->addRule(Form::MAX, 'Must be less than or equal to %d', 101)
151151
->addRule(Form::RANGE, 'Must be in range from %d to %d', [$minInput, $maxInput]);
152152

153-
Assert::same('<input type="number" name="count" id="frm-count" data-nette-rules=\'[{"op":":range","msg":"Must be in range from 0 to 100","arg":[0,100]},{"op":":min","msg":"Must be greater than or equal to 1","arg":1},{"op":":max","msg":"Must be less than or equal to 101","arg":101},{"op":":range","msg":"Must be in range from %0 to %1","arg":[{"control":"min"},{"control":"max"}]}]\' min="1" max="100">', (string) $input->getControl());
153+
Assert::same('<input type="number" name="count" min="1" max="100" id="frm-count" data-nette-rules=\'[{"op":":range","msg":"Must be in range from 0 to 100","arg":[0,100]},{"op":":min","msg":"Must be greater than or equal to 1","arg":1},{"op":":max","msg":"Must be less than or equal to 101","arg":101},{"op":":range","msg":"Must be in range from %0 to %1","arg":[{"control":"min"},{"control":"max"}]}]\'>', (string) $input->getControl());
154154
});
155155

156156

tests/Forms/Forms.renderer.1.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<tr class="required">
1515
<th><label for="frm-age" class="required">Your age:</label></th>
1616

17-
<td><input type="number" name="age" id="frm-age" required data-nette-rules='[{"op":":filled","msg":"Enter your age"},{"op":":integer","msg":"Please enter a valid integer."},{"op":":range","msg":"Age must be in range from 10 to 100","arg":[10,100]}]' min="10" max="100" class="text">
17+
<td><input type="number" name="age" min="10" max="100" id="frm-age" required data-nette-rules='[{"op":":filled","msg":"Enter your age"},{"op":":integer","msg":"Please enter a valid integer."},{"op":":range","msg":"Age must be in range from 10 to 100","arg":[10,100]}]' class="text">
1818

1919
<span class="error">
2020
Enter your age

tests/Forms/Forms.renderer.2.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<dt><label for="frm-age" class="required">Your age:</label></dt>
1515

16-
<dd class="odd"><input type="number" name="age" id="frm-age" required data-nette-rules='[{"op":":filled","msg":"Enter your age"},{"op":":integer","msg":"Age must be numeric value"},{"op":":range","msg":"Age must be in range from 10 to 100","arg":[10,100]}]' min="10" max="100" value="9.9" class="text"> •
16+
<dd class="odd"><input type="number" name="age" min="10" max="100" id="frm-age" required data-nette-rules='[{"op":":filled","msg":"Enter your age"},{"op":":integer","msg":"Age must be numeric value"},{"op":":range","msg":"Age must be in range from 10 to 100","arg":[10,100]}]' value="9.9" class="text"> •
1717

1818
<span class="error">
1919
Age must be numeric value

0 commit comments

Comments
 (0)