Php Larvel如何使用FormRequest验证多个文件(图像)
作为表单的一部分,我希望提交多达五个图像,并在带有自定义错误消息的FormRequest中验证它们 表单的文件提交部分如下所示:Php Larvel如何使用FormRequest验证多个文件(图像),php,laravel,laravel-5,Php,Laravel,Laravel 5,作为表单的一部分,我希望提交多达五个图像,并在带有自定义错误消息的FormRequest中验证它们 表单的文件提交部分如下所示: namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class StoreListingFormRequest extends FormRequest { /** * Determine if the user is authorized to make this re
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',
];
}
}
最多选择五个图像。。。
@如果($errors->has('images'))
{{$errors->first('images')}
@恩迪夫
我的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'
我到底做错了什么?我知道了
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>
最多选择五个图像。。。
@如果($errors->has('images'))
{{$errors->first('images')}
@恩迪夫
@如果($errors->has('images.*'))
{{$errors->first('images.*')}
@恩迪夫
文件或任何多输入验证错误都应检查密钥名称
它们的键名随数字(即:images.0)动态出现,并且它们都包含关键字“images”,因此我们可以使用它来捕获这些错误
因此,简单的检查应该完成以下工作:
@if($errors->has('images.*'))
@foreach($errors->get('images.*') as $key => $error)
<div class="error">{{ $errors->first($key) }}</div>
@endforeach
@endif
@if($errors->has('images.*))
@foreach($errors->get('images.*')作为$key=>$error)
{{$errors->first($key)}
@endforeach
@恩迪夫
我现在可以开始执行以下操作:“images.required'=>'请上载一个或多个图像”,“images.max'=>'最多允许五个图像',但数组项仍然没有任何内容我查看是否添加$errors->has('images.0'))我可以得到错误消息,但肯定有更好的方法。在“images.*”上,请删除所需内容并放置映像,同时尝试使用此sintax映像。*.file=>并让我知道这些场景的输出