laravel在服务器端验证后重定向时恢复我请求的值

时间:2017-04-19 09:51:45

标签: php validation laravel-5.4

当我尝试更新表单中的名称时,我正在使用laravel服务器端验证 并输入错误的密码,它重定向到同一页面并显示错误消息,但我试图更新的名称已经消失,并检索旧的名称..

我的控制器是:

 public function profileupdate(Request $request,$id)
    {
        if(Auth::Check())
        {

            $request_data = $request->All();
            $validator = $this->validator($request_data);
            if($validator->fails())
            {
            $this->throwValidationException($request, $validator); 
            }

        else
        {  
            $current_password = Auth::User()->password;   
            if(Hash::check($request_data['current-password'], $current_password))
            {
                $user_id = Auth::User()->id;                       
                $admin = Admin::find($user_id);        
                $admin->update([
                    'name'=>$request['name'],
                    'job_title'=> $request['job_title'],
                    'email'=>$request['email'],
                    'phone_number'=>$request['phone_number'],
                    'password'=> Hash::make($request['current-password']),
                ]);

                return  redirect('/admin/profile')
                 ->with('message', 'Admin Profile updated successfuly!');
            }
            else
            { 

               return   redirect('/admin/profile')
                ->with('password', 'Please enter correct password!'); 
            }
        }        
        }
        else
        {
            return redirect()->to('/');
        }    
    }

我的观点是:

 <form  action="{{ route('admin.profileupdate', Auth::user()->id) }}" method="get" class="form-horizontal" id="profile" name="profile">
                 {{csrf_field()}}
                 {{-- <input type="hidden" class="control-input" name="id"  value="{{ Auth::user()->id }}"/> --}}
                    <div class="control-group{{ $errors->has('name') ? ' has-error' : '' }}">
                    <label class="control-label">Name :</label>
                    <div class="controls">
                        <input type="text" class="control-input" name="name"  value="{{ Auth::user()->name }}"/>
                         @if ($errors->has('name'))
                                                <span class="help-inline">
                                                    <strong>{{ $errors->first('name') }}</strong>
                                                </span>
                             @endif
                    </div>
                    </div> 
                     <div class="control-group{{ $errors->has('job_title') ? ' has-error' : '' }}">
                    <label class="control-label">Role:</label>
                    <div class="controls">
                        <select class="control-select form-control" name="job_title" id="job_title" >
                            {{-- <option value="{{ Auth::user()->job_title }}">{{ Auth::user()->job_title }}</option>                               --}}
                            <option value="Admin" {{ (Auth::user()->job_title =="Admin") ? "selected" : "" }}>Admin</option>
                            <option value="Subadmin" {{ (Auth::user()->job_title =="Subadmin") ? "selected" : "" }}>Subadmin</option>
                            <option value="Superadmin" {{ (Auth::user()->job_title =="Superadmin") ? "selected" : "" }}>Superadmin</option>
                        </select>
                         @if ($errors->has('job_title'))
                                                <span class="help-inline">
                                                    <strong>{{ $errors->first('job_title') }}</strong>
                                                </span>
                             @endif                    </div>
                    </div>
                    <div class="control-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label class="control-label">Email:</label>
                            <div class="controls">
                              <input class="form-control" type="text" name="email" id="email" value="{{ Auth::user()->email }}" readonly>
                              @if ($errors->has('email'))
                                                <span class="help-inline">
                                                    <strong>{{ $errors->first('email') }}</strong>
                                                </span>
                             @endif
                            </div>
                          </div>
                    <div class="control-group{{ $errors->has('phone_number') ? ' has-error' : '' }}">
                    <label class="control-label">Mobile Number :</label>
                    <div class="controls">
                        <input type="text" class="control-input" name="phone_number" id="phone_number"  value="{{ Auth::user()->phone_number }}"/>
                         @if ($errors->has('phone_number'))
                                                <span class="help-inline">
                                                    <strong>{{ $errors->first('phone_number') }}</strong>
                                                </span>
                             @endif
                    </div>
                    </div>
                    <div class="control-group{{ $errors->has('current-password') ? ' has-error' : '' }}">
                            <label class="control-label"> Current Password:</label>
                            <div class="controls">
                               <input type="password" class="form-control" id="current-password" name="current-password" placeholder="Password">
                              @if(Session::has('password')) <span class="help-inline"> <strong>{{Session::get('password')}} </strong></span> @endif
                              @if ($errors->has('current-password'))
                                                <span class="help-inline">
                                                    <strong>{{ $errors->first('current-password') }}</strong>
                                                </span>
                             @endif
                            </div>
                    </div>
                    <div class="control-group">
                            <div class="controls">
                                <a href="{{ route('admin.password') }}">Change password</a>
                            </div>
                    </div>
                    <div class="form-actions">
                       <a href="{{ route('admin.dashboard') }}" class="btn btn-danger" >Cancel</a>
                       <button type="submit" class="btn btn-success">Update</button>
                    </div>
                </form>

如果我输入正确的密码错误

,请帮助我获取我输入的名称值

1 个答案:

答案 0 :(得分:0)

您的控制器功能必须返回错误和输入数据 -

从此处获取参考:Laravel Back to page with old input for validation

 public function profileupdate(Request $request,$id)
        {
            if(Auth::Check())
            {

                $request_data = $request->All();
                $validator = $this->validator($request_data);
                if($validator->fails())
                {
                $this->throwValidationException($request, $validator); 
                }

            else
            {  
                $current_password = Auth::User()->password;   
                if(Hash::check($request_data['current-password'], $current_password))
                {
                    $user_id = Auth::User()->id;                       
                    $admin = Admin::find($user_id);        
                    $admin->update([
                        'name'=>$request['name'],
                        'job_title'=> $request['job_title'],
                        'email'=>$request['email'],
                        'phone_number'=>$request['phone_number'],
                        'password'=> Hash::make($request['current-password']),
                    ]);

                    return  redirect('/admin/profile')
                     ->with('message', 'Admin Profile updated successfuly!');
                }
                else
                { 

                  // return   redirect('/admin/profile')
                   // ->with('password', 'Please enter correct password!'); 

return Redirect::to('/admin/profile')->withInput()->withErrors($validation->messages());

                }
            }        
            }
            else
            {
                return redirect()->to('/');
            }    
        }