php - Laravel: Form with files sent via Ajax -


im trying send form php server laravel validation says forms empty. trying upload photo albums. form consists of title(text), cover(image), , photos(image). receive 422 error (unprocessable entity). below code

html

<form class="lajax" action="{{ action('albumcontroller@store') }}" method="post">     <div class="form-group">         <label>album name</label>         <input type="text" name="name" class="form-control">                                                     </div>      <div class="form-group">         <label for="coverfile">album cover image</label>         <input  name="cover" type="file" id="coverfile">         <p class="help-block">example block-level text here.</p>     </div>      <div class="form-group">         <label for="albumfiles">album images</label>         <input type="file" name="photos[]" multiple>     </div>      <button type="submit" class="btn btn-primary">create album</button> </form>  

the jquery responsible ajax //object fed jquerys ajax method

            var ajax_options={                     url: url,                     method: method,                     beforesend: function(jqxhr,settings){                         console.log(jqxhr);                         console.log(settings);                         if(optns.debug)                             console.log('executing beforesend function');                         optns.lajaxbeforesend($form,formdata,jqxhr,settings);                     },                     success: function(data,textstatus,jqxhr){                         if(optns.debug)                             console.log('executing success function');                         optns.lajaxsuccess($form,formdata,data,textstatus,jqxhr)                     },                     error: function(jqxhr,textstatus,errorthrown){                         if(optns.debug)                             console.log('error encountered. ajax error function procked');                         optns.lajaxerror($form,formdata,jqxhr,textstatus,errorthrown);                          var errors = jqxhr.responsejson;                         console.log(errors);                     },                 }                  //check if files included in submitted form if method not                 if($form.find('input:file').length && method!='get'){                     ajax_options.processdata=false;                     ajax_options.contenttype=false;                     ajax_options.cache=false;                     ajax_options.data=formdata;                 }                   if(optns.debug)                     console.log('about send ajax request');                  //sending request here                 $.ajax(ajax_options); 

the laravel php file

    public function store(request $request)     {       //request input verification rules       $rules=[         'name'=>'required',         'cover'=>'required|image',         'photos'=>'required|array',         'photos.*'=>'image'       ];        //perform validation       $this->validate($request,$rules);        //rest of code     } 

try adding enctype="multipart/form-data" files=true form tag


Comments