Skip to content

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

@NoNoNo

Description

@NoNoNo

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];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions