php-Laravel请求验证在版本5.2中抛出HttpResponseException

我正在尝试在我的项目中将Laravel从5.1版本更新到5.2,我从文档中遵循了upgrade guide,但是现在当验证失败时我得到了HttpResponseException

 * Handle a failed validation attempt.
 *
 * @param  \Illuminate\Contracts\Validation\Validator  $validator
 * @return mixed
 *
 * @throws \Illuminate\Http\Exception\HttpResponseException
 */
protected function failedValidation(Validator $validator)
{
    throw new HttpResponseException($this->response(
        $this->formatErrors($validator)
    ));
}

在5.1中,该框架会因验证错误而自动重定向到先前的url.

这是我的验证请求

namespace domain\funcao\formRequest;

use autodoc\Http\Requests\Request;

class StoreFuncaoRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'codigo' => 'required|max:255|unique:funcao,codigo,'.$this->input('id').',id,deleted_at,NULL',
            'nome' => 'required|max:255|unique:funcao,nome,'.$this->input('id').',id,deleted_at,NULL'
        ];
    }
}

我已经按照指南更新了Exceptions Handler.

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\Access\AuthorizationException\AuthorizationException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Foundation\ValidationException\ValidationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
    ];
    ...
}

有人遇到这个问题吗?

解决方法:

我找到了导致此错误的原因,我在Exception Handler类中从Whoops手动调用了PrettyPageHandler.

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\Access\AuthorizationException\AuthorizationException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Foundation\ValidationException\ValidationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
    ];

    ...

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        // I just needed to remove this call to get rid of the problem
        if (config('app.debug'))
        {
            return $this->renderExceptionWithWhoops($e);
        }

        return parent::render($request, $e);
    }

    /**
     * Render an exception using Whoops.
     * 
     * @param  \Exception $e
     * @return \Illuminate\Http\Response
     */
    protected function renderExceptionWithWhoops(Exception $e)
    {
        $whoops = new \Whoops\Run;
        $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());

        return new \Illuminate\Http\Response(
            $whoops->handleException($e),
            $e->getStatusCode(),
            $e->getHeaders()
        );
    }    
}

我仍在使用Whoops,但现在会自动通过Laravel Exceptions

上一篇:作业帮助,我在这里做错了什么? [JavaScript,验证]


下一篇:c#-使用实体框架在WPF MVVM中进行验证