2

I have used validator from laravel but I want to add a custom rule if the user is not a authenticated user. Here is what I have done:

  public function postSignIn(){
    $validator = Validator::make(Input::all(),
        array(
            'email'     => 'required|email',
            'password'  => 'required'
        )
    );
    if($validator->fails()){
        //return error message
        return Redirect::to('account/signin')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    }
    else{
        $remember = (Input::has('remember')) ? true:false;
        $auth = Auth::attempt(array(
                'email'     => Input::get('email'),
                'password'  => Input::get('password'),
                'active'    => 1
            ),$remember
        );

        if($auth){
            return Redirect::intended('/')
                        ->with('global', 'Welcome');
        }
        else{
            return Redirect::to('account/signin')
                ->withErrors($validator, 'login');
        }
    }

And this is my view to display the error

     @if ($errors -> has())
         <div class="alert-danger alert-dismissible fade in" role="alert" style="padding: 0.5em;">
             <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
              @foreach($errors->all() as $error)

                   {{ $error }}<br />
              @endforeach

          </div>
     @endif

The validation for form works fine but I am having problem to display the error for unauthenticated user. So, any help would be appreciated very much.

2
  • This might help you: sitepoint.com/… Commented Dec 28, 2014 at 6:40
  • But I want to pass the error in the same $error message of the default laravel if the user has provided invalid username or password. Commented Dec 28, 2014 at 6:50

3 Answers 3

1

Be late but another solution to add custom message in error message bag is:

Controller

$rules = array(
        'email' => 'required|exists:users,email|email|max:32',
        'password' => 'required|max:20'
    );
    $validator = Validator::make($request->all(),$rules);


    $email = $request->email;
    $password = $request->password;

    $validateUser = new user();
    $users = $validateUser::where('email', $email)->get();
    if($users->isEmpty()){
        $validator->getMessageBag()->add('email', 'Invalid Email Address');    
        return redirect('home')->withErrors($validator);
    }
    foreach ($users as $user) {
        $data = $user->showAdminData();
        if($user->role_id!=1){
            $validator->getMessageBag()->add('email', 'Unauthorised access');
        }
        if(Crypt::decrypt($user->password)!==$password){
            $validator->getMessageBag()->add('password', 'Invalid Password');
        }
    }
    return redirect('home')->withErrors($validator);

View

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">E-Mail Address</label>

                        <div class="col-md-6">
                            <input type="email" class="form-control" name="email" value="{{ old('email') }}">

                            @if ($errors->has('email'))
                            <span class="help-block">
                                <strong>{{ $errors->first('email') }}</strong>
                            </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">Password</label>

                        <div class="col-md-6">
                            <input type="password" class="form-control" name="password">

                            @if ($errors->has('password'))
                            <span class="help-block">
                                <strong>{{ $errors->first('password') }}</strong>
                            </span>
                            @endif
                        </div>
                    </div>
Sign up to request clarification or add additional context in comments.

1 Comment

This is the clean way i searched for.
0

Try like this :

     <?php $message = Session::get('message'); $errors = Session::get('errors'); $status = Session::get('status'); ?>

      @if($message)

              <div class="infobox {{$message['type']}}-bg animated flipInY" style="margin-top:30px;">
                      //your functionalities
              </div>

      @endif

Comments

0

This may help achieve your goal. I have used the below code for custom error in my login form when user fails to authenticate.

    //Code in controller
   if(Auth::attempt(Input::only('email','password')))
        {

            return Redirect::to('/');
        }
       //put your message in a session
        Session::flash('msg', "Invalid email or password. Please Try again! ");
        return Redirect::back();    
   //Code in view
      <div class="error">
        @if (Session::has('msg'))
            {{ Session::get('msg') }}
        @endif
    </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.