Laravel - Check name of named error bag
Asked Answered
C

2

5

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();
}
Chauncey answered 5/7, 2018 at 8:36 Comment(2)
Does this help? laracasts.com/discuss/channels/general-discussion/… Looks like you can name the error bag: protected $errorBag = 'login';Probst
No, I can differentiate the error message, but I need to get the name of error bag.Chauncey
R
9

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.

Reclusion answered 5/7, 2018 at 8:56 Comment(4)
Did you change the key you're using to register? I put registration in my example above.Reclusion
Yes, I changed to register.. (I'm using register)Chauncey
Try using dd($errors->getBags()) that should show you which bags are available at that moment. It should return an array, keyed by the identifier for each bag.Reclusion
To access contents of the bag... @error('email', 'register') <strong>{{ $message }}</strong> @enderrorAmaurosis
M
0

Your Tab link should be like below :

if /auth is route for your page then

for login tab : /auth#login => this will display login tab

for register tab : /auth#register => this will display register tab

   <ul class="nav nav-tabs tab-nav-right" role="tablist">
         <li role="presentation" class="active"><a href="#login" data-toggle="tab"></a></li>
         <li role="presentation"><a href="#register" data-toggle="tab"></a</li>
  </ul>

    <!-- Tab panes -->
        <div class="tab-content">
          <div role="tabpanel" class="tab-pane fade in active" id="login">
                <b>Login Form</b>


               @if (Request::path()=='auth#login' && count($errors) > 0)
                   @foreach ($errors->all() as $error)
                       <p class="alert alert-danger alert-dismissible fade show" role="alert">{{ $error }}
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                         <span aria-hidden="true">&times;</span>
                        </button>
                        </p>
                   @endforeach
                @endif

          </div>
          <div role="tabpanel" class="tab-pane fade" id="register">
                <b>Register Form</b>

                @if (Request::path()=='auth#register' && count($errors) > 0)
                   @foreach ($errors->all() as $error)
                       <p class="alert alert-danger alert-dismissible fade show" role="alert">{{ $error }}
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                         <span aria-hidden="true">&times;</span>
                        </button>
                        </p>
                   @endforeach
                @endif
          </div>

in your controller for login tab error:

if ($validator->fails()) {
        return  redirect('/auth#login')
                    ->withErrors($validator, 'login')
                    ->withInput();
    }

in your controller for register tab error:

if ($validator->fails()) {
        return  redirect('/auth#register')
                    ->withErrors($validator, 'register')
                    ->withInput();
    }
Moribund answered 5/7, 2018 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.