From c96521a6e65a566fc148c917318052c30c87025e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Mike=C5=A1?= Date: Mon, 18 Nov 2019 21:44:20 +0100 Subject: [PATCH] Validator::formatMessage() Fixed using label placeholder in validation rule with Html label (#235) --- src/Forms/Validator.php | 9 ++++++--- tests/Forms/Controls.TextInput.render.phpt | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index cb618a4fe..1b8157510 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -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]; diff --git a/tests/Forms/Controls.TextInput.render.phpt b/tests/Forms/Controls.TextInput.render.phpt index faf0ff443..a4ce4d5e6 100644 --- a/tests/Forms/Controls.TextInput.render.phpt +++ b/tests/Forms/Controls.TextInput.render.phpt @@ -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('', (string) $input->getLabel()); + Assert::same('', (string) $input->getLabel(Html::el('b', 'Another label'))); + Assert::type(Html::class, $input->getControl()); + Assert::same('', (string) $input->getControl()); +}); + + test(function () { // password $form = new Form; $input = $form->addPassword('password')