user3127109
user3127109

Reputation: 3763

Adding custom error in Laravel?

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.

Upvotes: 2

Views: 3546

Answers (3)

Chintan7027
Chintan7027

Reputation: 7615

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>

Upvotes: 1

Basheer Kharoti
Basheer Kharoti

Reputation: 4302

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>

Upvotes: 0

MD. Atiqur Rahman
MD. Atiqur Rahman

Reputation: 2173

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

Upvotes: 0

Related Questions