gpt4 book ai didi

php - 在验证期间仅显示一次多个错误消息

转载 作者:搜寻专家 更新时间:2023-10-31 21:03:38 25 4
gpt4 key购买 nike

我的一个页面上有一个简单的多项选择题答案表。所有字段都是必填字段,我正在使用验证来检查它们是否全部填写正确。

当我返回错误消息时,我不想说哪个答案是错误的,我希望他们自己解决这个问题。但可以告诉他们还没有填写哪个具体问题。

鉴于此,我构建了我的验证器:

$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 (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

关于php - 在验证期间仅显示一次多个错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36660463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com