【问题标题】:Keep modal open after validation redirect验证重定向后保持模式打开
【发布时间】:2014-05-10 05:04:11
【问题描述】:

我目前正在开发一个通过模式框处理登录/注册的项目(所以我单击登录按钮,一个漂亮的模式会显示一个表单)。

我正在使用 Foundation 5 的显示模式来容纳我的登录表单,但是当提交表单并且出现验证错误时,模式会关闭。发生这种情况的原因是因为我正在重定向回登录表单所在的路径,并且在该路径中需要单击一个按钮来触发模式。

我想知道的是,我是否可以设置一些东西,以便在出现验证错误或异常(未找到帐户等)时保持打开模式。所以如果出现验证错误,模式保持打开状态。

寻找任何类型的解决方案。我的代码如下所示。

登录功能

    public function postLogin()
   {

    // Declare the rules for the form validation
    $rules = array(
        'email'    => 'required|email',
        'password' => 'required|between:3,32',
    );

    // Create a new validator instance from our validation rules
    $validator = Validator::make(Input::all(), $rules);

    // If validation fails, we'll exit the operation now.
    if ($validator->fails())
    {
        // Ooops.. something went wrong
        return Redirect::back()->withInput()->withErrors($validator);
    }

    try
    {
        // Try to log the user in
        Sentry::authenticate(Input::only('email', 'password'), Input::get('remember-me', 0));

        // Get the page we were before
        $redirect = Session::get('loginRedirect', 'dashboard');

        // Unset the page we were before from the session
        Session::forget('loginRedirect');

        // Redirect to the users page
        return Redirect::to($redirect)->with('success', Lang::get('auth/message.signin.success'));

    }

    catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
    {
        $this->messageBag->add('email', Lang::get('auth/message.account_not_found'));
    }
    catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
    {
        $this->messageBag->add('email', Lang::get('auth/message.account_not_activated'));
    }
    catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
    {
        $this->messageBag->add('email', Lang::get('auth/message.account_suspended'));
    }
    catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
    {
        $this->messageBag->add('email', Lang::get('auth/message.account_banned'));
    }

    // Ooops.. something went wrong
    return Redirect::back()->withInput()->withErrors($this->messageBag);
}

登录模式

<div id="myModalLogin" class="reveal-modal small" data-reveal>

<h2>Login</h2>

    <form method="post" action="{{ route('login') }}">


    {{-- CSRF Token --}} 
    <input type="hidden" name="_token" value="{{ csrf_token() }}" />

    {{-- Email --}} 

    <label for="email"> Email
        <input type="text" name="email" id="email" value="{{ Input::old('email') }}" />
    </label>
    {{ $errors->first('email', '<label class="error">:message</label>') }}

   {{-- Password --}}

    <label for="password"> Email
        <input type="password" name="password" id="password" value=""/>
    </label>
    {{ $errors->first('password', '<label class="error">:message</label>') }}  

    {{-- Remember me --}} 
    <input name="remember-me" value="1" id="remember-me" type="checkbox"><label for="remember-me">Remember me</label>

    <hr>

    {{-- Form Actions --}} 
    <button type="submit" class="button">Sign in</button>
    <a href="{{ route('forgot-password') }}">I forgot my password</a>        
    <a class="close-reveal-modal">&#215;</a>

</div>

【问题讨论】:

    标签: php laravel zurb-foundation


    【解决方案1】:

    您可以在返回验证结果时return false;

    【讨论】:

      【解决方案2】:

      如果您希望模态框自动打开,您需要创建一个标志变量并将其设置为true,如果您不想打开它,请将其设置为false

      问题在于-&gt;with() 不适用于Redirect::back(),因此我们需要一个解决方法:让我们将标志变量作为输入传递。为此,您必须获取所有旧输入并将新标志变量添加到它们。确保密钥(您的标志变量名称)不存在。您可以通过var_dump(Input::all()) 进行检查。

      $input = Input::all();//Get all the old input.
      $input['autoOpenModal'] = 'true';//Add the auto open indicator flag as an input.
      return Redirect::back()
          ->withErrors($this->messageBag)
          ->withInput($input);//Passing the old input and the flag.
      

      现在,在您看来,您必须将这个“旧”输入打印到您的 JavaScript 条件中。如果存在,它将打印其值:true,否则将打印第二个参数:false

      <script>
      $(document).ready(function () {
          if ({{ Input::old('autoOpenModal', 'false') }}) {
              //JavaScript code that open up your modal.
          }
      });
      </script>
      

      【讨论】:

      • 我都试过了,但似乎还是不行。您提供的第二个选项绝对是我正在寻找的解决方案,只是无法让它工作$(document).ready(function () { if ({{ (isset($autoOpenModal) &amp;&amp; $autoOpenModal) ? 'true' : 'false' }}) { $('#myModalLogin').foundation('reveal', 'open'); } }); 试过这个,但它不起作用。
      • 是的,模式在进入控制台时可以工作。包含 jQuery,并且没有控制台错误。我注意到,当我查看源代码时,代码显示如下$(document).ready(function () { if (false) { $('#myModalLogin').foundation('reveal', 'open'); } });,即使在我收到验证错误后它仍然保持错误。
      • @totymedi 是的,这是我的重定向 return Redirect::back()-&gt;withInput()-&gt;withErrors($validator)-&gt;with('autoOpenModal', true); 但即使在重定向之后它仍然是错误的。
      • @jackthedev 我发现Redirect::back() 不适用于-&gt;with()。用新的工作解决方案更新了我的答案。
      猜你喜欢
      • 2023-03-13
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 2014-08-29
      • 2014-04-26
      相关资源
      最近更新 更多