[英]Laravel validator throws an exception instead of redirecting back
升級到 Laravel 5.2 后,我遇到了 laravel 驗證器的問題。 當我想驗證控制器中的數據時,以這段代碼為例。
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class ContactController extends Controller
{
public function storeContactRequest(Request $request)
{
$this->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文檔的示例。 您可以使用 Validator facade,用於自定義驗證失敗行為
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Store the blog post...
}
這就是我在 Laravel 5.3 中處理它的方式(通過修改Handler.php
)
對於 laravel 5.2,我必須添加這一行:
if ($e instanceof ValidationException)
{
return redirect()->back()->withInput();
}
在 App\Exceptions\Handler.php 中,以及以下標頭:
use Illuminate\Session\TokenMismatchException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\AuthenticationException;
出於我的目的,我在Laravel 5.3中構建了一個完全基於 API 的應用程序,這是我從 Laravel 5.1 手動升級的。 我只需要 Laravel 響應需要修復我的 FormRequest 的驗證錯誤。
添加此行:
elseif ($e instanceof ValidationException)
{
return $this->convertValidationExceptionToResponse($e, $request);
}
在這個之后:
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
在App\Exceptions\Handler.php
中為我做了這個把戲,並在使用 FormRequest 驗證時返回了預期的驗證錯誤。
請在此處查看我 的評論:@ratatatKE 在 github 上的評論
可能會節省一些時間,另一個問題是您在視圖中而不是在控制器中調用validator->validate()
我在視圖中調用是因為我有一個在視圖上觸發的延遲加載組件
將 4.2 升級到 5.3 時,我遇到了同樣的問題。
這個答案對我有用。
覆蓋 app/Exceptions/Handler.php 中的方法
protected function convertExceptionToResponse(Exception $e)
{
if (config('app.debug')) {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
return response()->make(
$whoops->handleException($e),
method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500,
method_exists($e, 'getHeaders') ? $e->getHeaders() : []
);
}
return parent::convertExceptionToResponse($e);
}
在這里找到答案: https ://laracasts.com/discuss/channels/laravel/whoops-20-laravel-52
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.