Laravel Validation: Working With Error Messages

  • Illuminate\Support\MessageBag.

Retrieving The First Error Message For A Field

$errors = $validator->errors();

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

Retrieving All Error Messages For A Field

Foreach ($errors->get('email') as $message) {
    //
}
Foreach ($errors->get('attachments.*') as $message) {
    //
}

Retrieving All Error Messages For All Fields

Foreach ($errors->all() as $message) {
    //
}

Determining If Messages Exist For A Field

If ($errors->has('email')) {
    //
}

Custom Error Messages

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

$validator = Validator::make($input, $rules, $messages);
$messages = [
    'same'    => 'The :attribute and :other must match.',
    'size'    => 'The :attribute must be exactly :size.',
    'between' => 'The :attribute value :input is not between :min - :max.',
    'in'      => 'The :attribute must be one of the following types: :values',
];

Laravel Validation: Working With Error Messages — Structure map

Clickable & Draggable!

Laravel Validation: Working With Error Messages — Related pages: