2014-03-31 37 views
2

我目前正在通过模态框处理登录/注册项目(所以我点击登录按钮和一个漂亮的模态显示窗体)。验证重定向后保持模态打开

我使用基金会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> 

回答

2

你需要创建一个标志变量,你将传递到您的视图,并设置true如果你想在模式自动打开和设置它false如果你不想打开它:

这个问题是,->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> 
+0

我试过但仍然似乎没有工作。你提供的第二个选项绝对是我正在寻找的解决方案,只是无法让它工作'$(document).ready(function(){if({{(isset($ autoOpenModal)&& $ autoOpenModal)'true': ''false'}}){('#myModalLogin')。foundation('reveal','open'); } });'试过了,它不起作用。 – jackthedev

+1

是的,进入控制台时,模式工作。包含jQuery,没有控制台错误。我注意到,当我查看源码时,代码显示为'$(document).ready(function()if(false){('#myModalLogin')。foundation('reveal','open') ; } });'即使在我得到验证错误后它仍然是假的。 – jackthedev

+0

@totymedi是这是我的重定向'返回Redirect :: back() - > withInput() - > withErrors($ validator) - > with('autoOpenModal',true);'但它仍然错误,即使重定向后。 – jackthedev

0

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