How do I have multiple custom errors messages? #122

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

Projects

None yet

3 participants

@Petah
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-
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
Petah commented Apr 7, 2013

Any updates?

@nickl-
Member
nickl- commented Apr 13, 2013

@petah update still pending...

@Petah Petah referenced this issue in vlucas/valitron Apr 23, 2013
Closed

Array Validation #8

@henriquemoody
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.

@henriquemoody henriquemoody added Support and removed Enhancement labels Nov 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment