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

Fixes using label placeholder in validation rule with Html label #235

Merged
merged 3 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Forms;

use Nette;
use Nette\Utils\Html;
use Nette\Utils\Strings;
use Nette\Utils\Validators;

Expand Down Expand Up @@ -69,9 +70,14 @@ 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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this way (replacing ternary operator with if with early return) is more readable intead of having 2 nested ternary operators, but i am opened to adapt it to desired codestyle needs - there is missing some coding standard tool to force code style :-).

$caption = $rule->control->translate($rule->control->getCaption());
return $caption instanceof Html
? $caption
: rtrim($caption, ':');
}
return null;
case 'value': return $withValue ? $rule->control->getValue() : $m[0];
default:
$args = is_array($rule->arg) ? $rule->arg : [$rule->arg];
Expand Down
13 changes: 13 additions & 0 deletions tests/Forms/Controls.TextInput.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ 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 <b>Label</b>"}]\'>', (string) $input->getControl());

});


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