Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mirovit's avatar

FormRequest in 4.3 - cannot display single error

Hi all,

I'm using the 4.3 version to test it out, but I have a problem that I don't seem to find the solution to.

When I use the new FormRequest object as shown in the video by Jeffrey, everything works as it should, I get the $errors variable, but I cannot display single error.

If I loop through $errors->all() I get all errors as a list, but when I do $errors->first('first_name') I don't get anything.

Some had the same problem? Any solutions?

0 likes
10 replies
bashy's avatar

Going from the Dev docs on Laravel

$messages = $validator->messages();

echo $messages->first('email');

Could also try getting all messages for one input (to debug)

foreach ($messages->get('email') as $message)
{
    //
}
JeroenNoten's avatar
Level 5

Unfortunately, it is not possible to do $errors->first() yet, when using a FormRequest. But chances are great that it is possible when Laravel 4.3 will be released. I guess that it is not possible because of this line: https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Http/FormRequest.php#L149

So I think you can override formatErrors() in your FormRequest to solve your problem for the time being (however, I did not try this myself, so I could be wrong).

kreitje's avatar

Yes you can override the formatErrors method in your FormRequest.

mirovit's avatar

I overwrote the method like this:

protected function formatErrors(Validator $validator)
 {
  return $validator->errors()->getMessages();
 }

and now I can use the $errors->first(); method.

Thanks for the answers :)

Devon's avatar

You could...

  • Access the first item as you would any array
  • Use the array_first() helper
  • Create a new collection from the errors
// Access it as an array...
$errors[0];

// Use the array_first() helper
$error = array_first($errors, $callback);

// Create a new collection...
$errors = new Collection(is_array($errors) ? $errors : [$errors]);
$errors->first();
mirovit's avatar

@Devon yes I can access it as a normal array, but when I have 10 or more fields I don't know for which field the error refers to, but I want to display it after the corresponding input

Devon's avatar

Updated my previous post to include array_first() helper method

JeffreyWay's avatar

I'd full expect to see $errors->first() come back in time for the official 4.3 release in November.

Devon's avatar

@mirovit: "Whoops, looks like " I made a mistake. :P

Collection isn't what you're looking for, but rather MessageBag...

use Illuminate\Support\MessageBag;

$errors = [
    'email' => [
        'Error 1',
        'Error 2'
    ],
    'username' => [
        'User Error 1',
    ]
];

$errors = new MessageBag(is_array($errors) ? $errors : [$errors]);

// Output: string(12) "User Error 1"
dd($errors->first('username'));

// Output: string(39) "<span class="form-error">Error 1</span>"
dd($errors->first('email', '<span class="form-error">:message</span>'));

// Output: string(0) ""
dd($errors->first('password'));

// Output: string(0) ""
dd($errors->first('password', '<span class="form-error">:message</span>'));

@JeffreyWay, One can only hope, it's a huge convenience when working in views... :)

Instead of formatMessages() calling $validator->errors()->all() it should just use $validator->errors()->getMessages().

Please or to participate in this conversation.