简体   繁体   English

AuthController发现验证错误时,如何重定向到“注册模式”(bootstrap modal)窗口?

[英]How to redirect to Registration modal(bootstrap modal) window when AuthController found validation error/'s?

I'm currently using Laravel built-in authentication system. 我目前正在使用Laravel内置身份验证系统。 But I'm not using any specific page for User registration, instead I'm using a bootstrap modal. 但是我没有使用任何特定的页面来注册用户,而是使用了引导模式。 My code works as I expected, but only when the registration information (input values) are correct. 我的代码可以正常工作,但是仅当注册信息(输入值)正确时才可以。

But I want to redirect to registration modal window when the input fields values are invalid and want to show errors. 但是,当输入字段值无效并想显示错误时,我想重定向到注册模式窗口。

I'm attaching my codes as well as images for better understanding: 我附上代码和图片,以更好地理解:

The code inside bootstrap modal window: 引导程序模式窗口中的代码:

<button type="button" class="mdl-button mdl-js-button mdl-js-ripple-effect" data-toggle="modal" data-target="#registerModal">Register</button>

<div class="modal registerModal" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel">User Registration</h4>
      </div>
      <form action="{{ route('register') }}" method="POST">
      <div class="modal-body">
          <div class="form-group">
            <div class="row">
              <div class="col-md-3"><label for="name">Name:</label></div>
              <div class="col-md-9"><input type="text" name="name" class="form-control" id="name" placeholder="Your name"></div>
            </div>
            <br>
            <div class="row">
              <div class="col-md-3"><label for="email">Email:</label></div>
              <div class="col-md-9"><input type="email" name="email" class="form-control" id="email" placeholder="Your email **This should be unique**"></div>
            </div>
            <br>
            <div class="row">
              <div class="col-md-3"><label for="password">Password:</label></div>
              <div class="col-md-9"><input type="password" name="password" class="form-control" id="password" placeholder="Your password"></div>
            </div>
            <br>
            <div class="row">
              <div class="col-md-3"><label for="password_confirmation">Confirm Password:</label></div>
              <div class="col-md-9"><input type="password" name="password_confirmation" class="form-control" id="password_confirmation" placeholder="Confirm your password"></div>
            </div>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Confirm Registration</button>
        {{ csrf_field() }}
      </div>
      </form>
    </div>
  </div>
</div>

Route: 路线:

Route::post('/auth/register', ['uses'=>'Auth\AuthController@postRegister', 'as'=>'register']);

The modal window on which I want to redirect: 我要重定向的模式窗口:

在此处输入图片说明

Thanks. 谢谢。

在这种情况下,您有两个选择,您可以进行Ajax身份验证并接收有关控制器发送的错误响应的验证数据,或者可以检查bs3模态事件,我想您可以使用以下方法实现一些目的: http:/ /getbootstrap.com/javascript/#js-events

bretanac93's answer is a much more correct way of doing it because the modal itself would stay open and the user won't be as frustrated seeing an entire page flash when values are incorrect; bretanac93的答案是一种更正确的方法,因为模式本身会保持打开状态,并且当值不正确时,用户不会沮丧地看到整个页面闪烁; that said it can be a lot of work to reimplement and add javascript logic/server validation logic ( it's probably worth doing ). 那表示重新实现并添加javascript逻辑/服务器验证逻辑可能需要大量工作(这可能很值得)。

But that said; 但是那 the issue is the modal does not reappear when you are returned to the page because of a validation error because in your current code the only thing to initialize the modal window is the button click itself. 问题是,由于验证错误,返回到页面时模态不会重新出现,因为在当前代码中,唯一可以初始化模态窗口的是单击按钮本身。 What you could do is add logic to redisplay the modal window on a validation failure. 您可以做的是添加逻辑,以在验证失败时重新显示模式窗口。

Something like this; 像这样的东西;

@if( $errors->isNotEmpty() )
  <script>
    $(function () {
       $('#registerModal').modal('show')
    })
  </script>
@endif

All so keep in mind your current registration modal doesn't have any validation display code to show the user which parts are throwing the validation error. 所有这些都请记住,您当前的注册模式没有任何验证显示代码来向用户显示哪些部分引发了验证错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM