如果不满足某个条件,我想重定向到视图。校验码:
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors('errors',"Mention file")
->withInput();
}
它将数组打印为:
Illuminate'Support'ViewErrorBag {#239
#bags: array:1 [
"Mention file" => Illuminate'Support'MessageBag {#240
#messages: array:1 [
0 => array:1 [
0 => "errors"
]
]
#format: ":message"
}
]
在view end,我正在做:
@if (count($errors) > 0)
<div style="margin-top: 10%;" class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
不打印消息提及文件
您可以使用->withErrors
传递字符串,或者使用->with
传递带有键"errors"的数组:
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors("Mention file")
->withInput();
}
或
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->with(["errors" => "Mention file"])
->withInput();
}
好的,我发现了问题。
从文件中:
,您可以使用withErrors方法将错误消息闪烁到会话。当使用此方法时,$errors变量将在重定向后自动与视图共享,
所以->withErrors('errors',"Mention file")
实际上是把$errors
转换成一个字符串,把事情搞砸了。执行->withErrors("Mention file")
只是在ErrorBag
中添加消息。所以正确的代码应该是:
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors("Mention file")
->withInput();
}