Smsma
283
2
Laravel

dropzone with laravel form

Posted 9 months ago by Smsma

this error message is appear when I try to create item

The images field is required.

I add images to dropzone but it return null

this is my form

{!! Form::open([ 'url' => route('items.store'), 'class' => ' form-horizontal', 'files' => 'true', 'enctype' => 'multipart/form-data'  ]) !!}
        <input id="imagesStr" name="images" type="hidden" value="">

        <div class="row">
            <div class="col-lg-6">
                <div class="portlet light bordered">
                    <div class="portlet-title">
                        <div class="caption font-red-sunglo">
                            <i class="icon-settings font-red-sunglo"></i>
                            <span class="caption-subject bold uppercase"> البيانات الرئيسية</span>
                        </div>
                    </div>
                    <div class="portlet-body form">
                        
                        <div class="form-group">
                            <label for="name" class="col-lg-3 control-label">اسم الوحدة</label>
                            <div class="col-lg-9">
                                <input id="name" name="name"  class="form-control" placeholder="اسم الوحدة" >
                            </div>
                        </div>
     </div>

           <div class="col-lg-12">
                <div class="portlet light bordered">
                    <div class="portlet-title">
                        <div class="caption font-green-sharp">
                            <i class="icon-settings font-green-sharp"></i>
                            <span class="caption-subject bold uppercase">{{ trans('admin.propertyImgs') }}</span>
                        </div>
                    </div>
                    <div class="portlet-body form">

                        <div class="dropzone dropzone-file-area" id="my-dropzone" style="margin-top: 50px;">
                            <h4 class="sbold">اسحب الصور إلى هنا أو اضغط لاختيار صور</h4>
                        </div>

                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="portlet light bordered">
                <div class="portlet-body form">
                </div>
                <div class="form-actions">
                    <div class="row">
                        <div class="col-lg-6 col-lg-offset-3">
                            <button type="submit" class="btn green btn-block">حفظ</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    {!! Form::close() !!}
@endsection

@section('scripts')
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAJve7ZMt3PvwUzwmJlvHaItGO5uVhEUIg&v=3.exp&language=ar&amp;libraries=places"></script>
    <script src="{{ URL::asset('public/admin/js/map.js') }}"></script>
    <script src="{{ URL::asset('public/admin/js/bootstrap-fileinput.js') }}"></script>
    <script src="{{ URL::asset('public/admin/js/select2.full.min.js') }}"></script>
    <script src="{{ URL::asset('public/admin/js/components-select2.min.js') }}"></script>
    <script src="{{ URL::asset('public/admin/js/dropzone.min.js') }}"></script>
    <script src="{{ URL::asset('public/admin/js/form-dropzone.min.js') }}"></script>
@endsection
   

in controller

public function store(Request $request)
    {
        $rules = [
            'name'           => 'required|max:191',
            'images'           => 'required',
            'images.*'         => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        
        ];

        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails()) {
            return redirect()->route('items.create')->withErrors($validator)->withInput();
        }
        $request['group_id'] = Auth::user()->group->id;
        $request['code'] = mt_rand(1000,100000);

        $created = Item::create($request->all());

        if($request->images) {
            foreach($request->images as $image){
                $name = UploadImage($image, mt_rand(1000,100000),'items');
                
                Photo::create([
                  'name' => $name,
                  'item_id' => $created->id,
                ]);
            }
        }

        return $created ?
            redirect()->back()->with('success', trans('admin.addOk')) :
            // route('items.createOptions', $created->id
            redirect()->route('items.create')->with('warning', trans('admin.addNo'));
    }


Please sign in or create an account to participate in this conversation.