Skip to content

Commit

Permalink
Validator::formatMessage() Fixed using label placeholder in validatio…
Browse files Browse the repository at this point in the history
…n rule with Html label (#235)
  • Loading branch information
JanMikes authored and dg committed Nov 18, 2019
1 parent e55a65c commit c96521a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Forms/Validator.php
Expand Up @@ -69,9 +69,12 @@ public static function formatMessage(Rule $rule, bool $withValue = true)
static $i = -1;
switch ($m[1]) {
case 'name': return $rule->control->getName();
case 'label': return $rule->control instanceof Controls\BaseControl
? rtrim($rule->control->translate($rule->control->getCaption()), ':')
: null;
case 'label':
if ($rule->control instanceof Controls\BaseControl) {
$caption = $rule->control->translate($rule->control->getCaption());
return rtrim($caption instanceof Nette\Utils\Html ? $caption->getText() : $caption, ':');
}
return '';
case 'value': return $withValue ? $rule->control->getValue() : $m[0];
default:
$args = is_array($rule->arg) ? $rule->arg : [$rule->arg];
Expand Down
12 changes: 12 additions & 0 deletions tests/Forms/Controls.TextInput.render.phpt
Expand Up @@ -62,6 +62,18 @@ test(function () { // Html with translator
});


test(function () { // Html with label placeholder in validation rule message
$form = new Form;
$input = $form->addText('text', Html::el('b', 'Label:'))
->addRule(Form::REQUIRED, 'Please fill in %label');

Assert::same('<label for="frm-text"><b>Label:</b></label>', (string) $input->getLabel());
Assert::same('<label for="frm-text"><b>Another label</b></label>', (string) $input->getLabel(Html::el('b', 'Another label')));
Assert::type(Html::class, $input->getControl());
Assert::same('<input type="text" name="text" id="frm-text" required data-nette-rules=\'[{"op":":filled","msg":"Please fill in Label"}]\'>', (string) $input->getControl());
});


test(function () { // password
$form = new Form;
$input = $form->addPassword('password')
Expand Down

0 comments on commit c96521a

Please sign in to comment.