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

How do I have multiple custom errors messages? #122

Closed
Petah opened this issue Mar 14, 2013 · 4 comments
Closed

How do I have multiple custom errors messages? #122

Petah opened this issue Mar 14, 2013 · 4 comments
Labels

Comments

@Petah
Copy link

Petah commented Mar 14, 2013

How do I have multiple custom errors messages with Respect Validation.

I have some input that I want to validate against multiple validators. And I want a custom error message for each validation.

This is what I tried:

try {
    Respect\Validation\Validator::create()
        ->key('foo',
            v::length(20)->setName('bar')->setTemplate('Custom length message.')
             ->alnum()->setName('baz')->setTemplate('Custom alnum message.')
        )
        ->assert([
            'foo' => 'Hello, world!',
        ]);
} catch (Respect\Validation\Exceptions\ValidationException $exception) {
    $errors = $exception->findMessages([
        'bar',
        'baz',
    ]);
    var_dump($errors);
}

The output is:

array (size=2)
  'bar' => string '' (length=0)
  'baz' => string 'Custom alnum message.' (length=21)

I expected it to output both custom error messages.

Idealy I could get an array of messages for 1 input like:

var_dump($exception->findMessages(['foo']));

Would give me:

array (size=1)
  'foo' => 
    array (size=2)
      0 => string 'Custom length message.' (length=22)
      1 => string 'Custom alnum message.' (length=21)
@nickl-
Copy link
Member

nickl- commented Mar 24, 2013

I started looking into this and then got side tracked.

The alternative function

getFullMessages();

Should also give the templated responses but it just display the generic results.

"\-These rules must pass for "Array"
  |-"Hello, world!" must have a length greater than 20
  \-"Hello, world!" must contain only letters (a-z) and digits (0-9)"

further investigation pending, thank you for your patience.

@Petah
Copy link
Author

Petah commented Apr 7, 2013

Any updates?

@nickl-
Copy link
Member

nickl- commented Apr 13, 2013

@Petah update still pending...

@henriquemoody
Copy link
Member

I'm sorry about our late, @Petah, but this feature is available since always, you can check it on version 0.3.9, which is the first version available by composer:

<?php

require 'vendor/autoload.php';

use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validator as v;

try {
    v::create()
        ->key(
            'foo',
            v::length(20)->alnum()
        )
        ->assert([
            'foo' => 'Hello, world!',
        ]);
} catch (ValidationException $exception) {
    $errors = $exception->findMessages([
        'length' => 'Custom length message.',
        'alnum' => 'Custom alnum message.',
    ]);
    print_r($errors);
}

Is that what you need? If not fell free to reopen this issue any time.

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

No branches or pull requests

3 participants