Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation rules with Nette\Utils\Html-Strings not rendered correctly in data-nette-rules #243

Closed
NoNoNo opened this issue Apr 6, 2020 · 0 comments

Comments

@NoNoNo
Copy link

@NoNoNo NoNoNo commented Apr 6, 2020

Environment

nette/forms version: v3.0.3
nette/utils version: v3.1.1
php version: PHP 7.4.4 (cli)
Reproducibility: always

Steps to Reproduce

require 'vendor/autoload.php';

use Nette\Forms\Form;
use Nette\Utils\Html;

$form = new Form;

$form->addPassword('password1', 'Password:')
	->addRule(Form::PATTERN, 'Must contain number', '.*[0-9].*');

$form->addPassword('password2', 'Password:')
	->addRule(Form::PATTERN, Html::el()->addHtml('Must contain number'), '.*[0-9].*');

echo $form; // renders the form

Expected Result

<form action="" method="post">

<table>
<tr>
	<th><label for="frm-password1">Password:</label></th>

	<td><input type="password" name="password1" pattern=".*[0-9].*" id="frm-password1" 
    data-nette-rules='[{"op":":pattern","msg":"Must contain number","arg":".*[0-9].*"}]' class="text"></td>
</tr>

<tr>
	<th><label for="frm-password2">Password:</label></th>

	<td><input type="password" name="password2" pattern=".*[0-9].*" id="frm-password2" 
    data-nette-rules='[{"op":":pattern","msg":"Must contain number","arg":".*[0-9].*"}]' class="text"></td>
</tr>
</table>

</form>

Please note: data-nette-rules are the same in INPUT[name="password1] and INPUT[name="password2].

Actual Result

<form action="" method="post">

<table>
<tr>
	<th><label for="frm-password1">Password:</label></th>

	<td><input type="password" name="password1" pattern=".*[0-9].*" id="frm-password1" 
    data-nette-rules='[{"op":":pattern","msg":"Must contain number","arg":".*[0-9].*"}]' class="text"></td>
</tr>

<tr>
	<th><label for="frm-password2">Password:</label></th>

	<td><input type="password" name="password2" pattern=".*[0-9].*" id="frm-password2" 
    data-nette-rules='[{"op":":pattern","msg":{"attrs":[]},"arg":".*[0-9].*"}]' class="text"></td>
</tr>
</table>

</form>

Please note the "msg":{"attrs":[]} in INPUT[name="password2"][data-nette-rules].

Note

Found a workaround:

diff --git i/src/Forms/Validator.php w/src/Forms/Validator.php
index 66acfdd..6406374 100644
--- i/src/Forms/Validator.php
+++ w/src/Forms/Validator.php
@@ -52,7 +52,7 @@ class Validator
        {
                $message = $rule->message;
                if ($message instanceof Nette\Utils\IHtmlString) {
-                       return $message;
+                       return (string)$message;

                } elseif ($message === null && is_string($rule->validator) && isset(static::$messages[$rule->validator])) {
                        $message = static::$messages[$rule->validator];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant