Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Check name of named error bag

My registration form and login form are on the same page, but in different tabs. By default the active tab is login. While registering if there is any errors, the error messages are returned and I can print the error message at the right place by using named error bag.

The problem is when the error is from the registration form, the active tab should be register. For this I need to check the named of laravel validation.

How can I do that??

COde for validation is:

if ($validator->fails()) {
    return back()
    ->withErrors($validator, 'register')
    ->withInput();
}
like image 749
Sunny Kumar Avatar asked Jul 05 '18 08:07

Sunny Kumar


People also ask

How do I check for validation errors in Laravel?

Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code. You may use the @error Blade directive to quickly determine if validation error messages exist for a given attribute.

How do I pass an error bag to the error directive?

If you are using named error bags, you may pass the name of the error bag as the second argument to the @error directive: When Laravel generates a redirect response due to a validation error, the framework will automatically flash all of the request's input to the session.

What is attribute placeholder in Laravel error messages?

Many of Laravel's built-in error messages include an :attribute placeholder that is replaced with the name of the field or attribute under validation.

What is the current_password rule in Laravel 9?

The field under validation must be numeric. The field under validation must match the authenticated user's password. This rule was renamed to current_password with the intention of removing it in Laravel 9. Please use the Current Password rule instead.


1 Answers

You could try using the ->hasBag() method on the $errors variable to check if a bag exists for the given key, which would allow you to output the relevant CSS class or whatever you need to display the correct tab. For example:

<div class="tab registration{!! $errors->hasBag('register') ? ' active' : '' !!}">

</div>

hasBag('register') will return true if there's an error bag present for the registration form, assuming you've set up your validation to define which bag to use for registration errors. That will allow you to select the correct tab.

like image 129
Jonathon Avatar answered Oct 13 '22 01:10

Jonathon