如何在Laravel中同时显示验证错误和其他错误?
在我的控制器中有很多验证。在验证它们之后,我检查会话中是否存在某个元素。如果该元素不存在,那么我将发送另一个错误。我想同时显示所有验证错误和其他错误如何在Laravel中同时显示验证错误和其他错误?,laravel,laravel-5,Laravel,Laravel 5,在我的控制器中有很多验证。在验证它们之后,我检查会话中是否存在某个元素。如果该元素不存在,那么我将发送另一个错误。我想同时显示所有验证错误和其他错误 $this->validate($request,[ 'other11' => 'nullable|image', 'other12' => 'nullable|image', 'other13' => 'nullable|image', 'other14'
$this->validate($request,[
'other11' => 'nullable|image',
'other12' => 'nullable|image',
'other13' => 'nullable|image',
'other14' => 'nullable|image',
'other15' => 'nullable|image',
]);
if(session()->get('media')['other10']==NULL)
{
return back()->withErrors(['other10'=>'No data in session']);
}
当前,如果存在验证错误,则与“other10”字段相关的错误不会显示在视图中。是否有方法将验证错误和关于“other10”的错误一起返回到视图
$this->validate($request,[
'other11' => 'nullable|image',
]);
如果出现任何错误消息,这将重定向回验证失败。之后,按如下方式在视图中打印消息:
@if ($errors->has('other11'))
{{ $errors->first('email') }}
@endif
如果要打印所有邮件,这将帮助您:
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
如果出现任何错误消息,这将重定向回验证失败。之后,按如下方式在视图中打印消息:
@if ($errors->has('other11'))
{{ $errors->first('email') }}
@endif
如果要打印所有邮件,这将帮助您:
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
创建一个包含所有验证规则的验证器实例,然后您可以接受它的错误并添加任意数量的错误。大致如下:
$validator = Validator::make($request->all(), [
'other11' => 'nullable|image',
'other12' => 'nullable|image',
'other13' => 'nullable|image',
'other14' => 'nullable|image',
'other15' => 'nullable|image'
]);
$errors = $validator->errors();
if (session()->get('media')['other10'] == NULL) {
$errors->add('other10', 'No data in session');
}
return back()->withErrors($errors);
创建一个包含所有验证规则的验证器实例,然后您可以接受它的错误并添加任意数量的错误。大致如下:
$validator = Validator::make($request->all(), [
'other11' => 'nullable|image',
'other12' => 'nullable|image',
'other13' => 'nullable|image',
'other14' => 'nullable|image',
'other15' => 'nullable|image'
]);
$errors = $validator->errors();
if (session()->get('media')['other10'] == NULL) {
$errors->add('other10', 'No data in session');
}
return back()->withErrors($errors);
使用
而不是
return back()->withErrors(['other10'=>'No data in session']);
使用
而不是
return back()->withErrors(['other10'=>'No data in session']);
我知道如何显示错误消息。我想同时发送这两个错误。例如,如果存在验证错误,则无法显示关于other10字段的错误。$errors=$validator->errors();如果(session()->get('media')['other10']==NULL){$errors->add('other10','notdata in session');}这将限制发送特定的错误消息,不需要返回重定向回,laravel会自动将您重定向回,如果任何验证失败,这个问题会由于“Ahmed Nour Jamal El Din”而得到解决。但是为了将来,请告诉我如何在“validate”方法中编写关于“other10”字段的逻辑。我知道如何显示错误消息。我想同时发送这两个错误。例如,如果存在验证错误,则无法显示关于other10字段的错误。$errors=$validator->errors();如果(session()->get('media')['other10']==NULL){$errors->add('other10','notdata in session');}这将限制发送特定的错误消息,不需要返回重定向回,laravel会自动将您重定向回,如果任何验证失败,这个问题会由于“Ahmed Nour Jamal El Din”而得到解决。但是为了将来,请告诉我如何在“validate”方法中编写关于“other10”字段的逻辑。