4

Usually, when validating form requests in Laravel, we can access the errors using $validation->messages(). For example:

object(Illuminate\Support\MessageBag)#184 (2) { ["messages":protected]=> array(2) { ["email"]=> array(1) { [0]=> string(40) "The email must be a valid email address." } ["password"]=> array(2) { [0]=> string(41) "The password confirmation does not match." [1]=> string(43) "The password must be at least 6 characters." } } ["format":protected]=> string(8) ":message" }. 

Is there some elegant way to convert the object MessageBag to a sample array, such as:

[
object({"email" => "The email must be a valid email address."}),
object({"password" => "The password confirmation does not match."})
...
]

PS: If in the MessageBag, any field has more then one item, I would like only the first item in the resulting array of objects.

Thanks in advance.

4
  • 1
    It depends on your definition of elegant, but if you're asking if there's a single function to call on an MessageBag instance to get what you're looking for, I don't think there is. However you may be able to do it in something of a one-liner by mapping the messages stored within the bag like so: array_map(function ($item) { if (is_array($item)) { return reset($item); } return $item; }, $m->getMessages()); (In fact, because we know the messages are always an array you could remove the is_array conditional if you want to.) Commented Sep 13, 2015 at 14:53
  • I think so but hope to find more simple decision Commented Sep 13, 2015 at 16:22
  • Is it not possible to do $validation->messages()->toArray() ? You used to be able to do $validation->messages()->toJson(), so I am assuming toArray() would also work. Commented Sep 14, 2015 at 1:49
  • Its possible but in fact I'am getting another structure Commented Sep 14, 2015 at 16:27

2 Answers 2

8
$validation->messages()->all();
Sign up to request clarification or add additional context in comments.

8 Comments

@Evgeniy Try the toArray() method then. You can find all the methods available in the API documentation: laravel.com/api/master/Illuminate/Contracts/Support/…
I try this of course too. But some conditions from my question are not performed. I think it's impossible without any logic unfortunately :(
@Evgeniy You’re right. A field can have multiple errors, hence getting an array per key. Just use array_shift($messages) in PHP, or messages.shift() in JavaScript to get the first message for each field.
This what I get $response = []; foreach ($validator->messages() ->toArray() as $key => $value) { $obj = new \stdClass(); $obj->name = $key; $obj->message = $value[0]; array_push($response, $obj); }
@Evgeniy I really don’t know what you’re trying to show me.
|
2

Ok, something like this

$response = []; 
foreach ($validator->messages()->toArray() as $key => $value) { 
    $obj = new \stdClass(); 
    $obj->name = $key; 
    $obj->message = $value[0];

    array_push($response, $obj); 
}

It is not elegant but I do not see another way :)

Comments

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.