作为表单的一部分,我要提交最多五张图像,并在FormRequest中使用自定义错误消息对它们进行验证。
表单的文件提交部分如下所示:
<div id="dzone" class="form-group dropzone {{ $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
</div>
我的FormRequest看起来像这样:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreListingFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
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|mimes:jpg,jpeg,png,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.min' => 'Please upload one or more images',
'images.max' => 'A maximum of five images are allowed',
'images.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'images.*.max' => 'Sorry! Maximum allowed size for an image is 2MB',
];
}
}
以下几项不适用于图像验证:
首先,如果我在images数组上设置min:1,则如果我不提交任何图像,则不会返回错误消息,但如果将其设置为2,它将返回我的自定义错误消息。
我无法获得任何错误消息以返回图像。 .mimes'或'images。 .max'
我在做什么错了?
答案 0 :(得分:0)
我知道了
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>
答案 1 :(得分:0)
应检查文件名或任何多输入验证错误的键名。
它们的键名动态地带有数字(即:images.0),并且所有键名都包含关键字“ images”,因此这是我们可以用来捕获这些错误的关键所在。
因此,简单的检查就可以完成工作:
@if($errors->has('images.*'))
@foreach($errors->get('images.*') as $key => $error)
<div class="error">{{ $errors->first($key) }}</div>
@endforeach
@endif