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

Validator::formatMessage: support dynamic error messages #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/Forms/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public static function exportRules(Rules $rules): array
}
$op = Nette\Utils\Callback::toString($op);
}
if (is_callable($rule->message)) {
continue;
}
if ($rule->branch) {
$item = [
'op' => ($rule->isNegative ? '~' : '') . $op,
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function isRequired(): bool
/**
* Adds a validation rule for the current control.
* @param callable|string $validator
* @param string|object $errorMessage
* @param string|callable|object $errorMessage
* @return static
*/
public function addRule($validator, $errorMessage = null, $arg = null)
Expand Down
9 changes: 9 additions & 0 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ class Validator
public static function formatMessage(Rule $rule, bool $withValue = true)
{
$message = $rule->message;
if (is_callable($message)) {
$params = Nette\Utils\Callback::toReflection($message)->getParameters();
if (isset($params[1])) {
$message = $message($rule->control, $rule->arg);
} else {
$message = $message($rule->control);
}
}

if ($message instanceof Nette\Utils\IHtmlString) {
return $message;

Expand Down
11 changes: 11 additions & 0 deletions tests/Forms/Helpers.exportRules.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

declare(strict_types=1);

use Nette\Forms\Controls\TextInput;
use Nette\Forms\Form;
use Nette\Forms\Helpers;
use Tester\Assert;
Expand Down Expand Up @@ -91,3 +92,13 @@ test(function () {
],
], Helpers::exportRules($input2->getRules()));
});


test(function () {
$form = new Form;
$input = $form->addText('text');
$input->addRule(Form::EMAIL, function (TextInput $input, $arg) {
return $input->getValue() . ' is not valid e-mail address.';
});
Assert::same([], Helpers::exportRules($input->getRules()));
});
28 changes: 27 additions & 1 deletion tests/Forms/Rules.formatMessage.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

declare(strict_types=1);

use Nette\Forms\Controls\TextInput;
use Nette\Forms\Form;
use Tester\Assert;

Expand Down Expand Up @@ -34,11 +35,36 @@ $form->addText('special', 'Label:')
->addRule(Form::EMAIL, '%label %value is invalid [field %name] %d', $form['special'])
->setDefaultValue('xyz');

$form->addText('function', 'Label:')
->setRequired()
->addRule(function (TextInput $input, string $arg) {
return strpos($input->getValue(), $arg) === false;
}, function (TextInput $input, string $arg) {
return "String “{$input->getValue()}” contains a letter “{$arg}”, which is not allowed";
}, 'a')
->setDefaultValue('banana');

$form->addText('functionWithoutArg', 'Label:')
->setRequired()
->addRule(function (TextInput $input) {
return strpos($input->getValue(), 'e') === false;
}, function (TextInput $input) {
return "String “{$input->getValue()}” contains a letter “e”, which is not allowed";
})
->setDefaultValue('orange');

$form->validate();

Assert::true($form->hasErrors());

Assert::same(['1 5', '5 1', '1 ', 'Label xyz is invalid [field special] xyz'], $form->getErrors());
Assert::same([
'1 5',
'5 1',
'1 ',
'Label xyz is invalid [field special] xyz',
'String “banana” contains a letter “a”, which is not allowed',
'String “orange” contains a letter “e”, which is not allowed',
], $form->getErrors());

Assert::same([], $form->getOwnErrors());

Expand Down