我在使-> withInput()和-> withErrors()起作用时遇到问题,以便在验证失败时可以在刀片中检索这些信息。
这是我在控制器中拥有的:
$rules = array(
'mobile' => 'required'
);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
$fieldsWithErrorMessagesArray = $validator->messages();
$fieldsWithErrorMessagesArray = $fieldsWithErrorMessagesArray->messages();
$formattedErrorArray = array();
foreach ($fieldsWithErrorMessagesArray as $key => $error) {
$formattedErrorArray[$key] = $error[0];
}
return redirect()->back()->withInput()->withErrors($formattedErrorArray);
}
但是在var_dump出现$ errors时,却出现在刀片中:
object(Illuminate\Support\ViewErrorBag)#267 (1) { ["bags":protected]=> array(0) { } }
如果我要去dd($ fieldsWithErrorMessagesArray),它将给我:
array:7 [▼
"mobile" => array:1 [▼
0 => "The mobile field is required."
]
]
我也尝试过这种方式:
$test = array(
'mobile' => 'No jobs found. Please try searching with different criteria'
);
return redirect()->back()->withInput()->withErrors($test);
这有效,但我不确定为什么另一个无效。使用此测试阵列,刀片文件如下所示:
object(Illuminate\Support\ViewErrorBag)#264 (1) { ["bags":protected]=> array(1) { ["default"]=> object(Illuminate\Support\MessageBag)#265 (2) { ["messages":protected]=> array(2) { ["mobile"]=> array(1) { [0]=> string(59) "No jobs found. Please try searching with different criteria" }} ["format":protected]=> string(8) ":message" } } }
这是我的刀片文件的外观:
@if ($errors->has('mobile'))
@foreach($errors->get('mobile') as $error)
<span class="validation_error">{{$error}}</span>
@endforeach
@endif
<div class="form-group">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text"><i
class="fas fa-mobile-alt text-info"></i>
</div>
</div>
<input type="number" class="form-control" value="{{old('mobile')}}" name="mobile"
placeholder="Mobile Number">
</div>
</div>
我也有var_dump来查看带回来的任何错误:
<?php
echo var_dump($errors) . "</br>";
?>
下面,我提供了Kernel.php文件的外观:
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\Authenticate::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
答案 0 :(得分:1)
为什么不只使用“自动重定向”:
$validator = Validator::make($request->all(), array(
'mobile' => 'required'
));
$validator->validate(); // automatically redirects if validation fails
您可以在现有的验证程序实例上调用validate
方法。如果验证失败,则会自动将用户重定向。
这是完整的文档:https://laravel.com/docs/master/validation#automatic-redirection
注1:确保Illuminate\View\Middleware\ShareErrorsFromSession
文件(full docs)的web
中间件组中列出了\app\Http\Kernel.php
中间件。
注意2:Laravel将检查会话数据中的错误,因此请检查要设置的\config\session.php
文件上SESSION_DRIVER
和.env
属性上的会话驱动程序。尝试使用其他驱动程序,然后检查其是否有效。