Solution 1 :

You have two actually different cases, validation and verification. If the verification error message uses modal, does the validation error message use it too?

NO (Short Answer)

if ($verify) {
    return redirect()->route('shop.index');
}

return redirect()->route('shop.login', ['status' => 'error']);
@if(request()->input('status') == 'error')
<div class="modal fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalCenterTitle">Error</h5>
            </div>
            <div class="modal-body">
                Login or password incorrect! Try again.
            </div>
        </div>
    </div>
</div>
@endif
@if(request()->input('status') == 'error')
<script type="text/javascript">
    $("#modal1").modal('show');
</script>
@endif

YES (Long Answer)

I don’t know what $this->validate does, but I assume it’s the same thing as validate method provided by the IlluminateHttpRequest object.

If you look at the default LoginController, you will find that the verification error message is the same as validation, using:

throw ValidationException::withMessages([
    $this->username() => [trans('auth.failed')],
]);

That means, you will have one action for both. But in your case, you separate them.

/******** VALIDATION ********/
// if validation fails, an exception will be thrown and 
// the proper error response will automatically be sent back to the user.
$input = $req->validate($req, [
    'email'    => ['required', 'email'],
    'password' => ['required'],
]);

/******** VERIFICATION ********/
$verify = Auth::attempt(['email' => $data['email'], 'password' => $data['password']]);

if ($verify) {
    return redirect()->route('shop.index');
}

// return redirect()->route('shop.login', ['status' => 'error']);
// or 
return redirect()->route('shop.login')
    ->with('status-verification', 'Login or password incorrect! Try again.');
@if($errors->any())
<div class="modal fade" id="modal-validation" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalCenterTitle">Error Validation</h5>
            </div>
            <div class="modal-body">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        </div>
    </div>
</div>
@endif

@if(session('status-verification'))
<div class="modal fade" id="modal-verification" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalCenterTitle">Error</h5>
            </div>
            <div class="modal-body">
                {{ session('status-verification') }}
            </div>
        </div>
    </div>
</div>
@endif
<script type="text/javascript">
    @if($errors->any())
        $("#modal-validation").modal('show');
    @endif

    @if(session('status-verification'))
        $("#modal-verification").modal('show');
    @endif
</script>

Solution 2 :

You can use this script where you want to call the modal.

<script type="text/javascript">
$("#modal1").modal();
</script>

or

<script type="text/javascript">
$("#modal1").modal('show');
</script>

Requires bootstrap and jquery libraries to work.

Solution 3 :

If you don’t want to use template, I think you should use ajax to retrive validation..
But I think you should definately use templates to make it simpler… Obviously, ajax can handle it, but I don’t think ajax is going to be right solution for these kind of problems. Sorry, as I am saying so much about template, but i found it dissapointing seeing you not using templates
Visit blade template and
Visit twig template

Problem :

When the user enters one or more wrong credentials, a modal appears. The problem is that I don’t know how I can make this connection from controller to view. Would an ajax be needed, probably?

Controller:

      public function login()
  {
    return view('login');
  }

  public function logar(Request $req)
  {
    $data = $req->all();
    $verify = Auth::attempt(['email' => $data['email'], 'password' => $data['password']]);
    $this->validate($req, [
      'email' => [
        'required',
        'email',
      ]
    ]);
    if ($verify) {
      return redirect()->route('shop.index');
    }
    return redirect()->route('shop.login');
  }

View:

                <form class="login-form needs-validation" method="POST" action="{{ route('loja.logar') }}" novalidate>
                    <meta name="csrf-token" content="{{ csrf_token() }}">
                    @csrf
                    <div class="form-group">
                        <input type="email" class="form-control w-50 mx-auto my-0 py-4" id="email" name="email" aria-describedby="emailHelp" placeholder="Email" required>
                    </div>
                    <div class="form-group">
                        <input type="password" class="form-control w-50 mx-auto my-0 py-4" id="password" name="password" placeholder="Senha" required>
                    </div>
                    <button type="submit" id="submit" name="submit" class="btn btn-outline-primary mt-2 py-3 px-4">Enviar</button>
                </form>

               <div class="modal fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                  <div class="modal-dialog modal-dialog-centered" role="document">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalCenterTitle">Error</h5>
                       </div>
                       <div class="modal-body">
                         Login or password incorrect! Try again.
                       </div>
                     </div>
                   </div>
                 </div>

Comments

Comment posted by Julius Fasema

if i understand you clearly, are you checking the error here if ($verify) { return redirect()->route(‘shop.index’); } return redirect()->route(‘shop.login’);

Comment posted by Felipe Moreira

Thanks bro. I used the second method which, despite being longer, was more worth it!

Comment posted by Felipe Moreira

Yes. I want the modal to appear if the user enters the wrong credentials. Where should I place the javascript code? Directly on the Controller?

Comment posted by Biplove Lamichhane

But, in that case in first reload also, the modal will be showing… if I am not wrong!!! And @FelipeMoreira Why are you not using any template??

Comment posted by Felipe Moreira

Are you talking about the blade template? If so, I don’t know why or why to apply it.

Comment posted by Biplove Lamichhane

@FelipeMoreira Template, they are very important to send data from views and to template in the form of dictionary … Then you can retrieve it easily… In your problem… if you have used template…

Comment posted by Penny Codes

Use exceptions and handle it in your controller. It should work fine, assuming you get what I’m putting across.

By