1

I am passing three files and few other strings in my post request to the controller. But i don't know the reason why my validation fails.

Here is the form

<form>
  <input type="radio"  name="inter_fit" id="inter_good_fit" value = "good" >
  <input type="radio" name="inter_fit" id="inter_bad_fit" value = "bad">
  <input id="report_upload" type="file"/>
  <input id="skype_upload" type="file"/>
  <input id="audio_upload" type="file"/>
  <input type="hidden" id="comp_candidate_id"/>
  <input type="hidden" id="comp_profile_id"/>
</form>

var candidate_id =  $('#comp_candidate_id').val();
var profile_id =  $('#comp_profile_id').val();
var inter_fit = $("input[name=inter_fit]:checked").val();
var report_file = $("#report_upload").prop('files');
var skype_file = $("#skype_upload").prop('files');
var audio_file = $("#audio_upload").prop('files');
var dataString = 'inter_fit='  +inter_fit+ '&report_file='  +report_file+ '&skype_file='  +skype_file+ '&audio_file='  +audio_file+ '&candidate_id=' +candidate_id+ '&profile_id=' +profile_id;
console.log(dataString);
var formData = new FormData();
formData.append("inter_fit",inter_fit);
formData.append("candidate_id",candidate_id);
formData.append("profile_id",profile_id);
var reportInput = $("#report_upload").get(0).files[0];

formData.append("report_file",reportInput);              

var skypeInput = $("#skype_upload").get(0).files[0];
formData.append("skype_file",skypeInput);              

var audioInput = $("#audio_upload").get(0).files[0];
formData.append("audio_file",audioInput); 

             $.ajax({

                    type: "POST",
                    url: "/complete_interview",
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(data) {
                        showStatus(data)

                    },
                    error : function(xhr ,status ,error)
                    {
                        console.log(xhr);
                        console.log(status);
                        console.log(error);

                    }
                });

                function showStatus(data)
                {
                    console.log(data);
                }

When small files like text files are uploaded no problem occurs and it passes the validation. When the large files are uploaded the validation fails. Here is my controller code

public function completeInterview(Request $request){

        $val = \Validator::make($request->all(),
        ['inter_fit' => 'required',
        'report_file' => 'required',
        'skype_file' => 'required',
        'audio_file' => 'required', //not passed
        'candidate_id' => 'required',
        'profile_id' => 'required',
        ]);

        if ($val->fails()) {            
        return response()->json(['msg'=>"val_failed"]);
      }
      }

I have changed the PHP file upload and max post size configuration. I don't understand what is the issue here.

I always get the response as val_failed.

Naveen Kumar
  • 1,476
  • 5
  • 28
  • 52
  • 1
    Why don't you actually print out the validation errors so you know why it's failing? `$errors = $val>errors(); dd($errors->all());` – Brian Glaz Feb 14 '17 at 20:41

2 Answers2

0

I don't know if this answer will solve your question but, add in your form element the enctype like this:

<form role="form" method="post" enctype="multipart/form-data">

When you make a POST request, you have to encode the data that forms the body of the request in some way. See this question (and answer) for more about enctype.

Furthermore, add

'audio_file' => 'required|mimes:mpga',

to the validate section.

diakosavvasn
  • 854
  • 1
  • 8
  • 22
0

The Form::open method has 'files' => true that means form data will be encoded as "multipart/form-data" so it is required whenever you are going to upload any files with form data.

Example: http://www.expertphp.in/article/laravel-53-upload-image-with-validation-example