Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

enexspecial's avatar

How can i solve this error:422 (Unprocessable Entity)

Here is my javascript code:

$("#dis-form").on("submit", function(e){ e.preventDefault(); var token = $('input[name=token]').val(); var interest_id = $('#interest_id').val(); var title = $('#title').val(); var picturevideo = $('#picturevideo').val(); var body = $('#body').val(); var url = $(this).attr('action'); var post = $(this).attr('method');

$.ajax({
      type     : post,
      url      : url,
      data     : {'token':token,'interest_id':interest_id,'title':title,'picturevideo':picturevideo,'body':body },
      contentType : "application/json",
      success: function(data){
        console.log(data);
      },
      error: function(data){
        console.log(data);
      }
});

}); Here is my controller public function postDiscuss(Request $request) { $this->validate($request, [ 'interest' =>'required', 'title' => 'required', 'picturevideo' => 'required|mimes:jpg,jpeg,gif,mp4,mpeg,avi|max:5000000', 'body' => 'required' ]);

  if($request->ajax())
  {
    $discuss = new Discuss();
    $dicuss->user_id = Auth::user()->id;
    $discuss['interest_id'] = $request['interest_id'];
    $discuss['title'] = $request->title;
    if(Input::hasFile('picturevideo')){
      $file             = Input::file('picturevideo');
      $allowedfileTypes = config('app.allowedfileTypes');
      $filename         = $file->getClientOrginalName();
      $destinationPath  = config('app.fileDestinationPath').'/'.$filename;
      $uploded          = Storage::put($destinationPath, file_get_contents($file->getRealPath()));
      if($uploaded){
        $discuss['picturevideo'] = $request->$filename;
      }
    }
    $discuss['body'] = $request->body;
    if($discuss->save()){
        return response($discuss);
    }
  }
}

Please how can i solve this issues

0 likes
6 replies
tykus's avatar
tykus
Best Answer
Level 104

You are getting a 422 because validation is failing. You can check your request in the browser's devtools to see what is being sent in the request payload and dd($request->all()) to see what is being received by the server. Compare these to your validation rules, and you'll find the culprit.

Most likely this is because you are not actually uploading a file over AJAX, which does not support multipart/form-data by default. Take a look at this reference for the FormData API which is supported by modern browsers. https://developer.mozilla.org/en-US/docs/Web/API/FormData

enexspecial's avatar

Here is the form:

{{Form::Open(['action'=> 'campaignController@postDiscuss', 'id'=>'dis-form' ,'method'=>'post','files'=>true])}}
                  <div class="form-group">
                    {{Form::label('interest', 'Topic of Interest',['class'=>'control-label'])}}
                    <div class="">
                      {{Form::select('interest_id',$interest,null,['id'=>'interest_id', 'class'=>'form-control'])}}
                    </div>
                  </div>
                  <div class="form-group">
                    {{Form::label('title', 'Give a Title',['class'=>'control-label'])}}
                    <div class="">
                        {{Form::text('title','',['class'=>'form-control input-sm', 'id'=>'title'])}}
                    </div>
                  </div>
                  <div class="form-group">
                    {{Form::label('picturevideo', 'Upload',['class'=>'control-label'])}}
                    <div class="">
                      <input type="file" name="picturevideo" id="picturevideo" class="form-control input-sm">
                    </div>

                  </div>

                  <div class="form-group">
                    {{Form::label('body', 'Let\'s Talk',['class'=>'control-label'])}}
                    <div class="">
                        {{Form::textarea('body','',['id'=>'body','class'=>'form-control input-sm', 'rows' => '10'])}}
                    </div>
                  </div>
                  <button type="submit" class="btn btn-primary">Start Discussion</button>
                {{Form::Close()}}
Snapey's avatar

please learn to format code blocks

what possible benefit is seeing the form? Did you read @tykus reply at all?

enexspecial's avatar

ok i have read the page but and i have added the enctype = "multipart/form-data"

i did a console.log(data.responseJSON.message);

i got this error message [The given data was invalid.] So please explain to me cause i don't understand it anymore

tykus's avatar

The given data was invalid. is Laravel's standard error message whenever validation fails.

You cannot simply add enctype = "multipart/form-data" and expect this to work for AJAX requests - if you want to send multipart data over AJAX, then you need to use the FormData API like I said earlier. Have a look at this simple example

Please or to participate in this conversation.