验证重定向后保持模式打开 [英] Keep modal open after validation redirect

查看:74
本文介绍了验证重定向后保持模式打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事一个通过模式框处理登录/注册的项目(因此,我单击登录"按钮,并在表格中显示一个漂亮的模式).

i am currently working on a project where the login/register is handled through modal boxes (so i click the login button and a nice modal reveals with a form in).

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

Im using foundation 5's reveal modal to house my login form but when the form is submitted and theres a validation error the modal closes. The reason this is happening is because i am redirecting back to the route where the login form is and in that route a button needs to be clicked to fire the modal.

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

What i was wondering is, is there something i can set so that modal stays open if there is a validation error or exception (account not found etc.) So if there is a validation error the modal stays open.

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

looking for any type of solution. my code is shown below.

登录功能

    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>

推荐答案

如果您希望模式自动打开并将其设置为true. >如果您不想打开它:

You need to create a flag variable that you will pass to your view and set it true if you want the modal to auto open and set it false if you don't want to open it:

此问题是->with()不能与Redirect::back()一起使用,因此我们需要一种解决方法:让我们将flag变量作为输入传递.为此,您必须获取所有旧输入并将新的flag变量添加到它们中.确保键(您的标志变量名称)不存在.您可以使用var_dump(Input::all())进行检查.

The problem with this is that ->with() doesn't work with Redirect::back() so we need a workaround: lets pass our flag variable as an input. For this you have to get all the old input and add the new flag variable to them. Make sure that the key (your flag variable name) doesn't already exist. You can check this with a 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.

Now in your view you have to print this "old" input into your JavaScript condition. If it exists it will print its value: true, otherwise it will print the second argument: false.

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

这篇关于验证重定向后保持模式打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆