升级到Laravel 5.2后,我遇到了laravel验证器的问题.当我想验证控制器中的数据时,例如使用此代码.
validate($request, [ '_token' => 'required', 'firstname' => 'required|string' 'lastname' => 'required|string' 'age' => 'required|integer', 'message' => 'required|string' ]); // Here to store the message. } }
但不知何故,当我输入无效数据时,它不会将我重定向回上一页并向会话中闪现一些消息,但它会触发异常并给我一个500错误页面.
这是我得到的例外.我在文档中读到ValidationException是新的而不是HttpResponseException但我不知道它是否与此有关.
[2016-01-05 11:49:49] production.ERROR: exception 'Illuminate\Foundation\Validation\ValidationException' with message 'The given data failed to pass validation.' in /home/vagrant/Code/twentyre-webshop/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php:70
当我使用一个单独的请求类时,它只会重定向回错误消息.在我看来,只有控制器中使用的验证方法才会受到此行为的影响.
更新你的App\Exceptions\Handler
课程
use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Symfony\Component\HttpKernel\Exception\HttpException; use Illuminate\Foundation\Validation\ValidationException; /** * A list of the exception types that should not be reported. * * @var array */ protected $dontReport = [ AuthorizationException::class, HttpException::class, ModelNotFoundException::class, ValidationException::class, ];
我还建议您阅读如何迁移到laravel 5.2的文档,因为有一些重大更改.例如,这个ValidatesRequests
特性抛出
Illuminate\Foundation\Validation\ValidationException
而不是Illuminate\Http\Exception\HttpResponseException
记录如何从Laravel 5.1迁移到5.2