Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 Multiple Forms On A Single Page Name The Message Bag

I need help in Laravel 5.1 on how to name the MessageBag errors, allowing me to retrieve the error messages for a specific form.

This can be achieve in the store method of a controller without the using request.

Laravel 5.1 docs

return redirect('register')
            ->withErrors($validator, 'login');

But I am using a RegistrationRequest so I do not need to validate any input inside my controller I just need to set my rules in the request.

I tried this line of codes added to my request

/**
     * Format the errors from the given Validator instance.
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @return array
     */
    protected function formatErrors(Validator $validator)
    {
        return $validator->errors()->getMessages('registration');
    }
like image 785
HeisenBerg Avatar asked Dec 25 '22 17:12

HeisenBerg


1 Answers

I had the same problem, and when I looked in the FormRequest class, I found there was a property protected $errorBag = 'default'.

So, by overriding this propery in your request class, e.g.
protected $errorBag = 'login';

you can scope the errors from that request and then access them in your view like so:

$errors->login->has('email')

like image 58
JavaScript Warrior Avatar answered Dec 28 '22 07:12

JavaScript Warrior