当我有一个输入数组时,我很难检查验证错误,然后在Blade模板中获取错误。为了便于演示,我简化了HTML和PHP代码。
HTML代码:
<form>
<p>Title</p>
Female: <input type="text" name="title[female]">
Male: <input type="text" name="title[male]">
<p>Female Answers</p>
<input type="text" name="answer[female][]">
<input type="text" name="answer[female][]">
<input type="text" name="answer[female][]">
<p>Male Answers</p>
<input type="text" name="answer[male][]">
<input type="text" name="answer[male][]">
<input type="text" name="answer[male][]">
</form>
验证规则定义如下:
$rules = [
'title.*' => 'required|min:10|max:255',
'answer.females.*' => 'required',
'answer.males.*' => 'required'
];
问题是,当我试图在模板中查找这些错误时,我会得到一个错误,比如:
htmlspecialchar((期望参数1为字符串,数组给定〔…〕
我尝试了多种方法来检查错误,但都不起作用:
{{ $errors->has('title.' . $gender) ? 'form-error' : '' }}
{{ $errors->has('title')[$gender] ? 'form-error' : '' }}
{{ $errors->has('title.{$gender}') ? 'form-error' : '' }}
// Where $gender contains the gender as string, like "females", "males".
非常感谢您的帮助。
尝试这种格式的
{!! $errors->first('title.'. $gender , 'form-error') !!}