Php 如果用户输入的凭据不正确,则显示带有错误的模式

Php 如果用户输入的凭据不正确,则显示带有错误的模式,php,html,laravel,Php,Html,Laravel,当用户输入一个或多个错误凭据时,将出现一个模式。问题是我不知道如何从控制器连接到视图。可能需要ajax吗 控制器: public function login() { return view('login'); } public function logar(Request $req) { $data = $req->all(); $verify = Auth::attempt(['email' => $data['email']

当用户输入一个或多个错误凭据时,将出现一个模式。问题是我不知道如何从控制器连接到视图。可能需要ajax吗

控制器:

      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');
  }
视图:

                <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>

您可以在希望调用模态的位置使用此脚本

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


需要引导和jquery库才能工作。

如果您不想使用模板,我认为您应该使用ajax来检索验证。。 但我认为你应该明确使用模板来简化它。。。显然,ajax可以处理它,但我认为ajax不会是解决此类问题的正确方法。很抱歉,正如我所说的那个么多关于模板的内容,但我发现看到你们不使用模板让我很失望 和

您有两个实际不同的案例,验证和验证。如果验证错误消息使用模态,验证错误消息是否也使用模态

没有简短的回答 如果$verify{ 返回重定向->路由'shop.index'; } 返回重定向->路由'shop.login',['status'=>'error']; 这意味着,两种操作都有一个动作。但在你的情况下,你把它们分开

/******** 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.');

对我希望在用户输入错误凭据时显示模式。我应该把javascript代码放在哪里?直接在控制器上?但是,在第一次重新加载的情况下,模态将显示。。。如果我没有错!!!还有@FelipeMoreira你为什么不使用任何模板?你是说刀片模板吗?如果是这样的话,我不知道为什么或者为什么要应用它。@FelipeMoreira模板,它们对于从视图发送数据并以字典的形式发送到模板非常重要。。。然后你就可以很容易地取回它。。。在你的问题中。。。如果您使用了模板…请使用异常并在控制器中处理它。如果我明白你的意思,你是否在这里检查$verify{return redirect->route'shop.index';}return redirect->route'shop.login';谢谢兄弟。我使用了第二种方法,尽管更长,但更值得!
@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
/******** 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>