2 回答
TA贡献1810条经验 获得超5个赞
我想到了
public function rules()
{
return [
'title' => 'required|max:255',
'body' => 'required|max:2000',
'price' => 'required|max:100|regex:/^\d{1,13}(\.\d{1,4})?$/',
'contact_details' => 'required',
"images" => "required|array|min:1|max:5",
'images.*' => 'required|mimetypes:image/jpeg,image/png,image/bmp|max:2000',
'category_id' => [
'required',
\Illuminate\Validation\Rule::exists('categories', 'id')->where(function ($query) {
$query->where('usable', true);
})
],
'area_id' => [
'required',
\Illuminate\Validation\Rule::exists('areas', 'id')->where(function ($query) {
$query->where('usable', true);
})
]
];
}
public function messages()
{
return [
'contact_details.required' => 'At least one method of contact is required for your advert.',
'images.required' => 'Please upload one or more images',
'images.max' => 'A maximum of five images are allowed',
'images.*.mimetypes' => 'Only jpeg,png and bmp images are allowed',
'images.*.max' => 'Sorry! Maximum allowed size for an image is 2MB',
];
}
然后在视图中:
<div id="dzone" class="form-group dropzone {{ ($errors->has('images') || $errors->has('images.*')) ? ' has-error' : '' }}">
<div class="fallback">
<label for="images[]">Select up to five images...</label>
<input name="images[]" type="file" multiple />
</div>
@if ($errors->has('images'))
<span class="help-block">
{{ $errors->first('images') }}
</span>
@endif
@if ($errors->has('images.*'))
<span class="help-block">
{{ $errors->first('images.*') }}
</span>
@endif
</div>
TA贡献1865条经验 获得超7个赞
应检查文件或任何多输入验证错误的键名。
它们的键名动态地带有数字(IE:images.0),并且它们都包含关键字“images”,因此我们可以使用它来捕获这些错误。
所以,简单的检查应该做的工作:
@if($errors->has('images.*'))
@foreach($errors->get('images.*') as $key => $error)
<div class="error">{{ $errors->first($key) }}</div>
@endforeach
@endif
- 2 回答
- 0 关注
- 209 浏览
添加回答
举报