Skip to content

Commit

Permalink
TextInput: validators EMAIL, URL, INTEGER and FLOAT automatically set…
Browse files Browse the repository at this point in the history
…s type to 'email', 'url' or 'number' (BC break)
  • Loading branch information
dg committed May 27, 2016
1 parent 89a28e3 commit 72127b1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Forms/Controls/TextInput.php
Expand Up @@ -63,6 +63,10 @@ public function getControl()
foreach ($this->getRules() as $rule) {
if ($rule->isNegative || $rule->branch) {

} elseif ($input->type === 'text' && in_array($rule->validator, [Form::EMAIL, Form::URL, Form::INTEGER, Form::FLOAT], TRUE)) {

This comment has been minimized.

Copy link
@hrach

hrach May 27, 2016

Contributor

wouldn't be isset() on static types faster?

This comment has been minimized.

Copy link
@Majkl578

Majkl578 May 27, 2016

Contributor

True, 4x faster on 7.0, 10x faster on 5.6. But one would have to call it like 10000x to notice the difference.

This comment has been minimized.

Copy link
@hrach

hrach May 27, 2016

Contributor

The proper reason is the repeated alowed value list.

static $types = [Form::EMAIL => 'email', Form::URL => 'url', Form::INTEGER => 'number', Form::FLOAT => 'number'];
$input->type = $types[$rule->validator];

} elseif (in_array($rule->validator, [Form::MIN, Form::MAX, Form::RANGE], TRUE)
&& in_array($input->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], TRUE)
) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Forms/Controls.translate().phpt
Expand Up @@ -83,13 +83,13 @@ test(function () {

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

$email2 = $form->addText('email2')
->addRule($form::EMAIL, new StringWrapper('Your name'));
Assert::match('<input type="text" name="email2" id="frm-email2" data-nette-rules=\'[{"op":":email","msg":"StringWrapper"}]\'>', (string) $email2->getControl());
Assert::match('<input type="email" name="email2" id="frm-email2" data-nette-rules=\'[{"op":":email","msg":"StringWrapper"}]\'>', (string) $email2->getControl());
$email2->validate();
Assert::same(['StringWrapper'], $email2->getErrors());
});
2 changes: 1 addition & 1 deletion tests/Forms/Forms.renderer.1.expect
Expand Up @@ -14,7 +14,7 @@
<tr class="required">
<th><label for="frm-age" class="required">Your age:</label></th>

<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">
<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">

<span class="error">
Enter your age
Expand Down
2 changes: 1 addition & 1 deletion tests/Forms/Forms.renderer.2.expect
Expand Up @@ -13,7 +13,7 @@

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

<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"> •
<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"> •

<span class="error">
Age must be numeric value
Expand Down

0 comments on commit 72127b1

Please sign in to comment.