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

Setting a template for a rule with no effect #22

Closed
gus27 opened this issue Feb 16, 2017 · 3 comments
Closed

Setting a template for a rule with no effect #22

gus27 opened this issue Feb 16, 2017 · 3 comments
Milestone

Comments

@gus27
Copy link

gus27 commented Feb 16, 2017

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.:

    protected $use_templates = FALSE;
    ...
    $this->errors[implode('.', $actualKeys)] = ( $this->use_templates ? [$exception->getMainMessage()] : $exception->getMessages() );
@DavidePastore
Copy link
Owner

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)?

@gus27
Copy link
Author

gus27 commented Mar 21, 2017

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.

@gus27 gus27 closed this as completed Mar 21, 2017
DavidePastore added a commit that referenced this issue Mar 21, 2017
@DavidePastore DavidePastore modified the milestones: v0.5.0, future-release Mar 21, 2017
@DavidePastore
Copy link
Owner

You can find this feature in the new 0.5.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants