laravel-admin 多图上传

  1. form 方法内
        $form->multipleImage('image', '配图');
  2. 对应的 Model 内

    public function setImageAttribute($image)
    {
        if (is_array($image)) {
            $this->attributes['image'] = json_encode($image);
        }
    }
    
    public function getImageAttribute($image)
    {
        return json_decode($image, true);
    }

laravel laravel-admin

发布了94 篇原创文章 · 获赞 22 · 访问量 11万+
展开阅读全文

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

05-15

<div class="post-text" itemprop="text"> <p>As part of a form, I want to submit up to five images and validate them in a FormRequest with custom error messages. </p> <p>The file submit part of the form looks like this:</p> <pre class="lang-php prettyprint-override"><code><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> </code></pre> <p>My FormRequest looks like this:</p> <pre><code>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', ]; } } </code></pre> <p>There several things not working with the images validation:</p> <p>First, if I set min:1 on the images array it does not return an error message if I submit no images but if I set it to 2 it returns my custom error message.</p> <p>I cannot get any error messages to return for images.<em>.mimes' or 'images.</em>.max'</p> <p>what am I doing wrong here?</p> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览