Laravel 如何使用 FormRequest 验证多个文件(图像)

作为表单的一部分,我想提交最多五张图像并在带有自定义错误消息的 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);

            })

        ]

    ];

}



有几件事不适用于图像验证:


首先,如果我在图像数组上设置 min:1,如果我不提交任何图像,它不会返回错误消息,但如果我将其设置为 2,它会返回我的自定义错误消息。


我无法收到任何错误消息以返回图像。.mimes' 或 'images. 。最大限度'


我在这里做错了什么?


ITMISS
浏览 210回答 2
2回答

森栏

我想到了&nbsp; &nbsp;public function rules(){&nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'required|max:255',&nbsp; &nbsp; &nbsp; &nbsp; 'body' => 'required|max:2000',&nbsp; &nbsp; &nbsp; &nbsp; 'price' => 'required|max:100|regex:/^\d{1,13}(\.\d{1,4})?$/',&nbsp; &nbsp; &nbsp; &nbsp; 'contact_details' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; "images"&nbsp; &nbsp; => "required|array|min:1|max:5",&nbsp; &nbsp; &nbsp; &nbsp; 'images.*' => 'required|mimetypes:image/jpeg,image/png,image/bmp|max:2000',&nbsp; &nbsp; &nbsp; &nbsp; 'category_id' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \Illuminate\Validation\Rule::exists('categories', 'id')->where(function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->where('usable', true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'area_id' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \Illuminate\Validation\Rule::exists('areas', 'id')->where(function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->where('usable', true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ];}public function messages(){&nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; 'contact_details.required' => 'At least one method of contact is required for your advert.',&nbsp; &nbsp; &nbsp; &nbsp; 'images.required' => 'Please upload one or more images',&nbsp; &nbsp; &nbsp; &nbsp; 'images.max' => 'A maximum of five images are allowed',&nbsp; &nbsp; &nbsp; &nbsp; 'images.*.mimetypes' => 'Only jpeg,png and bmp images are allowed',&nbsp; &nbsp; &nbsp; &nbsp; 'images.*.max' => 'Sorry! Maximum allowed size for an image is 2MB',&nbsp; &nbsp; ];}然后在视图中:&nbsp;<div id="dzone" class="form-group dropzone {{ ($errors->has('images') || $errors->has('images.*')) ? ' has-error' : '' }}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="fallback">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="images[]">Select up to five images...</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="images[]" type="file" multiple />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if ($errors->has('images'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="help-block">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ $errors->first('images') }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if ($errors->has('images.*'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="help-block">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ $errors->first('images.*') }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>

鸿蒙传说

应检查文件或任何多输入验证错误的键名。它们的键名动态地带有数字(IE:images.0),并且它们都包含关键字“images”,因此我们可以使用它来捕获这些错误。所以,简单的检查应该做的工作:&nbsp;@if($errors->has('images.*'))&nbsp; &nbsp; &nbsp; @foreach($errors->get('images.*') as $key => $error)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="error">{{ $errors->first($key) }}</div>&nbsp; &nbsp; &nbsp; @endforeach&nbsp;@endif
打开App,查看更多内容
随时随地看视频慕课网APP