jackthedev
jackthedev

Reputation: 417

Keep modal open after validation redirect

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

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.

Login function

    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);
}

Login modal

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

Upvotes: 2

Views: 5851

Answers (2)

Lorena Pita
Lorena Pita

Reputation: 1516

You can return false; when you return the validations results.

Upvotes: 0

totymedli
totymedli

Reputation: 31108

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:

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.

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>

Upvotes: 3

Related Questions