Search code examples
phplaravellaravel-4cartalyst-sentry

Form Validation Laravel Session Data


I am trying to do form validation with sentry using laravel4. When I use the code shown below the errors are catched but the Session data does not seem to be sent back to the View to inform the user of the problem when logging in. Is there something I am doing wrong? I've been passing Session data between my views like this all through my application.

This is my Controller Action:

public function handleLogin() { 
    try{
        $credentials = array(
            'email'    => Input::get('email'),
            'password' => Input::get('password'),
        );

        $user = Sentry::authenticate($credentials, false);
        if($user){
            if($user->hasAccess('admin')){//Admin Base Controller - Dashboard Display welcome
                return Redirect::action('BookController@index')->with('message', $user->first_name . " " . $user->last_name);
            }else{//User Base Controller - Dashboard Display 
                return Redirect::to('user/dashboard/')->with('message', 'Welcome User');
            }
        }   
    //Error with logging in redirect to login page and display error
    }catch(Cartalyst\Sentry\Users\LoginRequiredException $e){
        return View::make('home.login')->with('message', 'Login field is required.');
    }catch(Cartalyst\Sentry\Users\PasswordRequiredException $e){
        return View::make('home.login')->with('message', 'Password field is required. ');
    }catch(Cartalyst\Sentry\Users\WrongPasswordException $e){
        return View::make('home.login')->with('message', 'Wrong password, try again.');
    }catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
        return View::make('home.login')->with('message', 'User was not found.');
    }catch (Cartalyst\Sentry\Users\UserNotActivatedException $e){
        return View::make('home.login')->with('message', 'User is not activated.');
    }
}

This is my View:

@if(Session::has('message'))
    <div class="alert alert-success alert-dissmissible" role="alert">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span>
        <span class="sr-only">Close</span>
        </button>
        <strong>Goodbye: </strong>{{ Session::get('message') }}
    </div>
@endif

@if(Session::has('success'))
    <div class="alert alert-success alert-dissmissible" role="alert">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span>
        <span class="sr-only">Close</span>
        </button>
        <strong>Success: </strong>{{ Session::get('success') }}
    </div>
@endif


    <div class="page-header">
        <h1>Login</h1>
    </div>  
    <!-- Login Form -->
    <form action="{{action('HomeController@handleLogin')}}" method="post">
        <div class="form-group">
            <label for="email">Email</label>
            <input type="text" class="form-control" name="email"/>
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" class="form-control" name="password"/>
        </div>
        <input type="submit" value="Login" class="btn btn-primary"/>
    </form>
@stop

Solution

  • A View::make()->with('foo', 'Error') passes $foo as variable instead of setting the Session variable.

    Perhaps you can try:

    @if(isset($message))
        <div class="alert alert-success alert-dissmissible" role="alert">
            <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
            </button>
            <strong>Goodbye: </strong>{{ $message }}
        </div>
    @endif