You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Setting a template for a rule (to specify your own error message) currently has no effect on the error messages you get with $errors = $request->getAttribute('errors');
Example: I have two regex rules and I'd like to prevent the default error message "xxx must validate against regex".
$aValidator = v::regex('/^[a-zA-Z]([-.a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}$/')->setTemplate('Hostname {{name}} is not valid');
$bValidator = v::regex('/^[a-zA-Z]$/')->setTemplate('Entry {{name}} should contain only letters');
$validators = array(
'a' => $aValidator,
'b' => $bValidator
);
$app->put('/entity', function ($request, $response, $args) {
if ($request->getAttribute('has_errors')) {
$errors = $request->getAttribute('errors');
// For an invalid entry of 'a' $errors contains here the
// default error message instead of the template message:
// array(1) { ["a"]=> array(1) {
// [0]=> string(86) ""1abfx53357" must validate against "/^[a-zA-Z]([-.a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}$/""
// } }
}
})->add(new DavidePastore\Slim\Validation\Validation($validators));
I have not seen any way to retrieve the error messages that are set by the setTemplate method. As far as I have tracked it down the issue could be in line 125 of Validation.php:
$this->errors[implode('.', $actualKeys)] = $exception->getMessages();
When you change this to $this->errors[implode('.', $actualKeys)] = [$exception->getMainMessage()];
the error messages contain the template text.
Did I overlook something?
If not it would be nice if these messages could be activated/changed by a switch, e.g.:
Hi @gus27 ! Thanks for your feedback. Could you please take a look to #23? It should do what you want. What do you think of it (you can find the documentation here)?
Hi @DavidePastore - Just checked out the use-template branch. It works like a charm. The new options parameter is a good idea to keep backwards compatibility.
Setting a template for a rule (to specify your own error message) currently has no effect on the error messages you get with
$errors = $request->getAttribute('errors');
Example: I have two regex rules and I'd like to prevent the default error message "xxx must validate against regex".
I have not seen any way to retrieve the error messages that are set by the
setTemplate
method. As far as I have tracked it down the issue could be in line 125 of Validation.php:$this->errors[implode('.', $actualKeys)] = $exception->getMessages();
When you change this to
$this->errors[implode('.', $actualKeys)] = [$exception->getMainMessage()];
the error messages contain the template text.
Did I overlook something?
If not it would be nice if these messages could be activated/changed by a switch, e.g.:
The text was updated successfully, but these errors were encountered: