0

In laravel 5.4 when validation is failed I do like:

if ($validator->fails()) {
                $errors_list = $validator->messages()->all();

and I got array like :

[errors_list] => Array
    (
        [0] => The image has already been taken.
        [1] => The is main has already been taken.
    )

What I dislike in this output that actuall name of error field is ommitted.

Code

echo '<pre>$validator->messages()::'.print_r($validator->messages(),true).'</pre>';

has output:

$validator->messages()::Illuminate\Support\MessageBag Object
(
    [messages:protected] => Array
        (
            [image] => Array
                (
                    [0] => The image has already been taken.
                )

            [is_main] => Array
                (
                    [0] => The is main has already been taken.
                )

        )

    [format:protected] => :message
)

And I did not find how access to messages data.

I would like to get array like:

[errors_list] => Array
        (
            [image] => The image has already been taken.
            [is_main] => The is main has already been taken.
        )

Is there is a way to make it ?

Thanks!

0

2 Answers 2

1

I hope this will work for you.

$errors->has('image');
$errors->get(image);

$errors->has('is_main');
$errors->get(is_main);
Sign up to request clarification or add additional context in comments.

Comments

0

It's a tricky one. The reason it's like that is because there may be multiple validation errors happening at once. However if you only have a single rule per entry:

array_combine($validator->messages()->keys(),$validator->messages()->all())

Comments

Your Answer

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