Skip to content

Commit 213cb58

Browse files
JanMikesdg
authored andcommitted
Validator::formatMessage() Fixed using label placeholder in validation rule with Html label (#235)
1 parent 12a72c9 commit 213cb58

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Forms/Validator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ public static function formatMessage(Rule $rule, bool $withValue = true)
6969
static $i = -1;
7070
switch ($m[1]) {
7171
case 'name': return $rule->control->getName();
72-
case 'label': return $rule->control instanceof Controls\BaseControl
73-
? rtrim($rule->control->translate($rule->control->getCaption()), ':')
74-
: null;
72+
case 'label':
73+
if ($rule->control instanceof Controls\BaseControl) {
74+
$caption = $rule->control->translate($rule->control->getCaption());
75+
return rtrim($caption instanceof Nette\Utils\Html ? $caption->getText() : $caption, ':');
76+
}
77+
return '';
7578
case 'value': return $withValue ? $rule->control->getValue() : $m[0];
7679
default:
7780
$args = is_array($rule->arg) ? $rule->arg : [$rule->arg];

tests/Forms/Controls.TextInput.render.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ test(function () { // Html with translator
6262
});
6363

6464

65+
test(function () { // Html with label placeholder in validation rule message
66+
$form = new Form;
67+
$input = $form->addText('text', Html::el('b', 'Label:'))
68+
->addRule(Form::REQUIRED, 'Please fill in %label');
69+
70+
Assert::same('<label for="frm-text"><b>Label:</b></label>', (string) $input->getLabel());
71+
Assert::same('<label for="frm-text"><b>Another label</b></label>', (string) $input->getLabel(Html::el('b', 'Another label')));
72+
Assert::type(Html::class, $input->getControl());
73+
Assert::same('<input type="text" name="text" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"Please fill in Label"}]\'>', (string) $input->getControl());
74+
});
75+
76+
6577
test(function () { // password
6678
$form = new Form;
6779
$input = $form->addPassword('password')

0 commit comments

Comments
 (0)