7

For array validation messages, is there a way to display the value as opposed to the attribute? Doing so without using a custom validator.

Example:

$messages = [
   ‘*' => ':value is invalid.’
]

This would output something like "email@address is invalid".

Thanks for your help!

2
  • Possible duplicate of Get access to value that failed validation, in a replacer? Commented Mar 14, 2016 at 18:23
  • If nothing else is available I may have to go down the custom validator route. Was hoping there was something simple like :value that could be used in the messages. Commented Mar 14, 2016 at 19:18

3 Answers 3

19

In case anyone is still looking with the latest of Laravel versions, the answer is to use the :input parameter in your message output:

'between' => 'The :attribute value :input is not between :min - :max.'

Docs: https://laravel.com/docs/5.7/validation#custom-error-messages

Sign up to request clarification or add additional context in comments.

1 Comment

also works with Laravel 9.x Validator::extend($name, $anonymousCallable, ':input is missing');. I wonder why it was not included in their documentation.
1

To access the index for the array validation I simply iterate over the elements I'm trying to validate instead of using the * wildcard.

public function messages()
{
    $messages = [];
    foreach($this->emails as $key => $email) {
        $messages[$key] = $email . ' is an invalid email address.';
    }

    return $messages;
}

Hope this helps anyone who is having the same problem.

1 Comment

Thanks, this will work while I am stuck in an old version of Laravel. It looks like a PR has since been merged for this in L9: github.com/laravel/framework/pull/41123
0

For fully custom strings you can pass custom messages as the third argument to the Validator::make() method. If you need only generic descriptors you can use some built place-holders such as :attribute, :size, or :values

For example:

$messages = ['required' => 'The :attribute field is required.'];

$validator = Validator::make($input, $rules, $messages);

:attribute will be replaced by the actual name of the field under validation.

More info can be found here.

4 Comments

Thanks for your reply. Ya I saw that this could be done, but I was wondering if there was a :value instead o :attribute. Attribute is the key where Im looking for the value.
Looking at the docs, I don't see a place-holder defined for the value but I think that's because it's available within the class where $messages would normally be defined. For instance: ['required' => 'The value ' . $this->email . ' cannot be empty.'];
I'm doing array validation, so I can access the array by using $this->emails, but I don't know which index is failing. How do you get the right value into the message. $messages = [‘*' => $this->emails[$indexHere] . 'is invalid.’]
figured out a way around it and posted an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.