Php 验证期间仅显示一次多个错误消息
我的一页上有一个简单的多选答案表。所有字段都是必需的,我正在使用验证来检查它们是否都已完成且正确 当我返回我的错误消息时,我不想说哪个答案是错误的,我希望他们自己能找到答案。但可以告诉他们哪些具体问题没有填写 鉴于此,我构建了如下验证程序:Php 验证期间仅显示一次多个错误消息,php,validation,laravel,laravel-5.1,Php,Validation,Laravel,Laravel 5.1,我的一页上有一个简单的多选答案表。所有字段都是必需的,我正在使用验证来检查它们是否都已完成且正确 当我返回我的错误消息时,我不想说哪个答案是错误的,我希望他们自己能找到答案。但可以告诉他们哪些具体问题没有填写 鉴于此,我构建了如下验证程序: $validator = Validator::make($request->all(), [ 'multi1' => 'required|in:b', 'multi2' => 'requir
$validator = Validator::make($request->all(), [
'multi1' => 'required|in:b',
'multi2' => 'required|in:a',
'multi3' => 'required|in:c',
'multi4' => 'required|in:b',
'multi5' => 'required|in:c',
'multi6' => 'required|in:a',
'multi7' => 'required|in:c',
'multi8' => 'required|in:c',
'multi9' => 'required|in:a',
'multi10' => 'required|in:b',
], [
'multi1.required' => 'The first question is empty!',
'multi2.required' => 'The second question is empty!',
'multi3.required' => 'The third question is empty!',
'multi4.required' => 'The fourth question is empty!',
'multi5.required' => 'The fifth question is empty!',
'multi6.required' => 'The sixth question is empty!',
'multi7.required' => 'The seventh question is empty!',
'multi8.required' => 'The eighth question is empty!',
'multi9.required' => 'The ninth question is empty!',
'multi10.required' => 'The tenth question is empty!',
'multi1.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi2.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi3.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi4.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi5.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi6.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi7.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi8.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi9.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
'multi10.in' => 'At least one of your answers was incorrect. Please review them and resubmit.',
]);
if ($validator->fails()) {
return redirect('/application/multichoice')
->withErrors($validator)
->withInput();
}
唯一的问题是,如果一个或多个问题不正确,那么相同的错误消息,至少一个答案不正确。请检查并重新提交。
,重复多次
我将它打印在页面上,如下所示:
@if (count($errors) > 0)
<div class="alert alert-danger">
<p><b>There are some errors with your answers:</b></p>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if(计数($errors)>0)
您的答案有一些错误:
@foreach($errors->all()作为$error)
- {{$error}}
@endforeach
@恩迪夫
有没有一种简单的方法可以防止这种情况发生,或者只打印一次?您可以跟踪以前的错误,只显示一个错误 如果与上一个错误不同:
@if (count($errors) > 0)
<div class="alert alert-danger">
<p><b>There are some errors with your answers:</b></p>
<ul>
{{--*/ $prev = null; /*--}}
@foreach ($errors->all() as $error)
@if ($prev != $error)
<li>{{ $error }}</li>
@endif
{{--*/ $prev = $error; /*--}}
@endforeach
</ul>
</div>
@endif
@if(计数($errors)>0)
您的答案有一些错误:
{{--*/$prev=null;/*--}
@foreach($errors->all()作为$error)
@如果($prev!=$error)
- {{$error}}
@恩迪夫
{{--*/$prev=$error;/*--}
@endforeach
@恩迪夫
我不得不将$prev
的声明更改为{{-*/$prev=null;/*-}}
,但除此之外,该操作非常有效!