【问题标题】:Laravel 5.1 Form Request ValidationLaravel 5.1 表单请求验证
【发布时间】:2016-03-11 10:06:50
【问题描述】:

当您在 laravel 中创建手动验证器时,您可以附加回调以在验证完成后运行,如下所示:

$validator = Validator::make(...);

$validator->after(function($validator) {
   if (!Auth::validate(['email' => Auth::user()->email, 'password' => $this->input('old_password')]))
   {
       $validator->errors()->add('old_password', 'Invalid password');
   }
});

如何在表单请求中执行相同的操作,即在下面的表单请求示例中,您在哪里添加上述回调函数?

class AccountRequest extends Request
{
     public function authorize()
     {
          return true;
     }

     public function rules()
     {
        return [
            'new_password' => 'required|confirmed|min:6';
            'old_password' => 'required';
        ];
     }

}

【问题讨论】:

    标签: validation laravel-5.1


    【解决方案1】:

    参考 cmets,你需要验证旧密码等于数据库中的现有字段,以及新密码 = 确认密码。

    为此,首先为旧密码构建一个客户验证器:

    <?php
    
    namespace App\Providers;
    
    use Validator;
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            Validator::extend('existing', function($attribute, $value, $parameters, $validator) {
                return  \Hash::check($value, User::find($parameters[0])->password);
            });
        }
    
        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    }
    

    然后在您的表单请求中:

    <?php
    
    namespace App\Http\Requests;
    
    use App\Http\Requests\Request;
    
    class FormRequest extends Request
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return false;
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'old' => 'existing:'.\Auth::user()->id,
                'password' => 'confirmed|required_with:old'
            ];
        }
    public function messages()
    {
        return [
            'old.existing' => 'Wrong old password'
        ];
    }
    
    
    }
    

    【讨论】:

    • 我已经尝试过了,但我得到了错误ReflectionException in Container.php line 572: Class App\Http\Requests\ValidationService does not exist
    • 问题来了,博客使用的是 Laravel 5 的 beta 版本,所以事情发生了变化。
    • 请问,您为什么需要它?实际上Controller中的代码是验证后应该运行的,那为什么需要在Request中呢?
    • 我想给用户更改密码的可能性,所以我有 3 个表单字段(new_password、new_password_confirmation、old_password)。在允许进行更新之前,我想使用存储在数据库中的内容检查用户的旧密码。
    • 你可以通过简单的表单请求来做到这一点,你不需要任何验证后
    【解决方案2】:

    您可以尝试添加public function after($request),看看是否有效。

    【讨论】:

      猜你喜欢
      • 2015-11-07
      • 2016-01-20
      • 2015-09-24
      • 2016-09-02
      • 2016-05-16
      • 2016-05-23
      • 2015-06-09
      • 1970-01-01
      相关资源
      最近更新 更多