Skip to content

Commit 72127b1

Browse files
committed
TextInput: validators EMAIL, URL, INTEGER and FLOAT automatically sets type to 'email', 'url' or 'number' (BC break)
1 parent 89a28e3 commit 72127b1

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

src/Forms/Controls/TextInput.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function getControl()
6363
foreach ($this->getRules() as $rule) {
6464
if ($rule->isNegative || $rule->branch) {
6565

66+
} elseif ($input->type === 'text' && in_array($rule->validator, [Form::EMAIL, Form::URL, Form::INTEGER, Form::FLOAT], TRUE)) {
67+
static $types = [Form::EMAIL => 'email', Form::URL => 'url', Form::INTEGER => 'number', Form::FLOAT => 'number'];
68+
$input->type = $types[$rule->validator];
69+
6670
} elseif (in_array($rule->validator, [Form::MIN, Form::MAX, Form::RANGE], TRUE)
6771
&& in_array($input->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], TRUE)
6872
) {

tests/Forms/Controls.translate().phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ test(function () {
8383

8484
$email = $form->addText('email')
8585
->addRule($form::EMAIL, 'error');
86-
Assert::match('<input type="text" name="email" id="frm-email" data-nette-rules=\'[{"op":":email","msg":"error"}]\'>', (string) $email->getControl());
86+
Assert::match('<input type="email" name="email" id="frm-email" data-nette-rules=\'[{"op":":email","msg":"error"}]\'>', (string) $email->getControl());
8787
$email->validate();
8888
Assert::same(['error'], $email->getErrors());
8989

9090
$email2 = $form->addText('email2')
9191
->addRule($form::EMAIL, new StringWrapper('Your name'));
92-
Assert::match('<input type="text" name="email2" id="frm-email2" data-nette-rules=\'[{"op":":email","msg":"StringWrapper"}]\'>', (string) $email2->getControl());
92+
Assert::match('<input type="email" name="email2" id="frm-email2" data-nette-rules=\'[{"op":":email","msg":"StringWrapper"}]\'>', (string) $email2->getControl());
9393
$email2->validate();
9494
Assert::same(['StringWrapper'], $email2->getErrors());
9595
});

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="text" 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]}]' class="text">
17+
<td><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">
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="text" 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]}]' value="9.9" class="text"> •
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"> •
1717

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

0 commit comments

Comments
 (0)