【问题标题】:Laravel how to validate multiple files (images) using FormRequestLaravel 如何使用 FormRequest 验证多个文件(图像)
【发布时间】:2019-10-02 21:57:14
【问题描述】:

作为表单的一部分,我想提交最多五张图像,并在 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',
    ];
}
}

图像验证无法正常工作:

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

我无法为图像返回任何错误消息。.mimes' 或 'images..max'

我在这里做错了什么?

【问题讨论】:

  • 我现在可以执行以下操作:'images.required' => '请上传一张或多张图片', 'images.max' => '最多允许五张图片'
  • 但数组项仍然没有
  • 我看看如果我添加 $errors->has('images.0') 我可以得到错误消息,但肯定有更好的方法。
  • on 'images.*' remove required and put image instead and also try using this sintax image.*.file => 让我知道这些场景的输出

标签: php laravel laravel-5


【解决方案1】:

我想通了

   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>

【讨论】:

    【解决方案2】:

    文件或任何多输入验证错误应检查键名。

    它们的键名随数字动态变化(IE:images.0),并且它们都包含关键字“images”,因此我们可以用来捕捉这些错误。

    所以,简单的检查就可以了:

     @if($errors->has('images.*'))
          @foreach($errors->get('images.*') as $key => $error)
               <div class="error">{{ $errors->first($key) }}</div>
          @endforeach
     @endif
    

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 1970-01-01
      • 2019-12-14
      • 2017-11-20
      • 2021-06-28
      • 2017-02-03
      • 2023-03-12
      • 1970-01-01
      • 2015-10-04
      相关资源
      最近更新 更多