Skip to content

Commit

Permalink
Validator::formatMessage: support dynamic error messages
Browse files Browse the repository at this point in the history
This is useful when you want to show user an informative error message, whose text
depends on some external data that depends on the value of some other field.

For example, my form allows user to choose a category of a team. Each category has a different
limit for number of allowed members, which is specified in config.neon. I want to show the user
the limit as part of the error message when she manages to exceed the limit.
  • Loading branch information
jtojnar committed Apr 10, 2019
1 parent d9fc6d6 commit b789d86
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
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

0 comments on commit b789d86

Please sign in to comment.