$ validator-> failed()对每个记录Laravel 5.2都无法正常工作

时间:2017-06-10 09:36:52

标签: validation authentication login laravel-5.2

auth脚本几天前工作正常,但现在它无法正常工作。 $ validator适用于数据库中的旧电子邮件记录,但不适用于新电子邮件。

以下是在AuthController.php中登录的代码:

   public function login(\Illuminate\Http\Request $request)
{
    $rules = [
        /*'email'    => 'required|email|unique:users,email',*/
        'email'    => 'required|email|exists:users,email',
        'password' => 'required',
    ];

    $input = Input::only('email', 'password');

    $validator = Validator::make($input, $rules);
    //return $input['email'];
    if ($validator->fails()) {
        /*$failedRules = $validator->failed();
        return $failedRules;*/

        /*if(isset($failedRules['email']['exists'])) {
            return 'dont exists';
        }*/
       return Redirect::to('/login')->withInput()->withErrors($validator);

    }

    $rules = [
        'hcptch' => 'required|hiddencaptcha',
    ];



    if($this->validate($request,$rules)){
        return Redirect::to('/login')->withInput()->withErrors($validator);
    }

    $credentials = [
        'email'     => Input::get('email'),
        'password'  => Input::get('password'),
        'confirmed' => 1,
    ];

    if (!Auth::attempt($credentials)) {

        //old_password login
        $user = User::where('email', Input::get('email'))->get();
        /*$user2 = \DB::table('users')->select('name')->where('email', Input::get('email'))->get();
        return $user[0]->password;*/
        if(md5(strtolower(Input::get('password'))) == $user[0]->old_password){
            $user[0]->password = Hash::make(Input::get('password'));
            $user[0]->old_password = null;
            $user[0]->save();

            if (!Auth::attempt($credentials)) {
                Session::flash('message', 'Welcome back!');
                return Redirect::home();
            }
        }

        return Redirect::back()
            ->withInput()
            ->withErrors(
                [
                    'password' => 'We were unable to sign you in. Incorrect Password',
                ]
            );
    }
    Session::flash('message','Welcome back!');

    return Redirect::home();
}

Login.blade.php

  <form id="form-login" class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
        <div class="title">LOGIN</div>
          {!! csrf_field() !!}
          <div class="col-sm-12">

          <div class="form-group{{ $errors->has('email') && !strpos(Request::fullUrl(), 'register') ? ' has-error' : '' }}">
             <label class="control-label">Email</label>
          <div>
         {!! Form::text('email',old('email'),['class'=>'form-control']) !!}
                @if ($errors->has('email') && !strpos(Request::fullUrl(), 'register'))
            <span class="help-block">
                  <strong>{{ $errors->first('email') }}</strong>
             </span>
         @endif
        </div>
      </div>
      <div class="form-group{{ $errors->has('password') && !strpos(Request::fullUrl(), 'register') ? ' has-error' : '' }}">
     <label class="control-label">Password</label>
     <div>
        {!! Form::password('password',['class'=>'form-control']) !!}
         @if ($errors->has('password') && !strpos(Request::fullUrl(), 'register'))
           <span class="help-block">
                <strong>{{ $errors->first('password') }}</strong>
            </span>
       @endif
       @if ($errors->has('hcptch') && !strpos(Request::fullUrl(), 'register'))
        <span class="help-block">
             <strong>{{ $errors->first('hcptch') }}</strong>
        </span>
        @endif
     </div>
   </div>

   <div class="form-group">
       <div class="clearfix">

          <button type="submit" class="btn btn-red font-av-heavy pull-left g-recaptcha" data-sitekey="6Le8jRoUAAAAABrE1Dpxo6ZZev4zeYKlk43eDf62" data-callback="loginOnSubmit">Login</button>
         <div class="checkbox pull-right">
               <a href="{{ url('/password/reset') }}" class="forgot_pass">Forgot Password?</a>
         </div>
       </div>
     </div>
    </div>
   <div class="clearfix"></div>
       {{ HiddenCaptcha::render() }}
  </form>

旧电子邮件记录的代码仍然正常,但新电子邮件地址出错“选定的电子邮件无效。”我已经尝试了几个小时来弄清楚问题是什么但没有得到任何线索。请帮助我,我做错了什么或错过了什么。谢谢

0 个答案:

没有答案