Reputation: 309
Someone please help ! This is a crazy error I am getting in Laravel. Ok i have a form and I am trying have .doc .pdf and a image file to get uploaded to the server. So in my html i deceived my in input as a file using the type attribute. When I try to submit the form using ajax new FormData() I see the file being sent in the header of the HTTPS request in the console but the response from the server does not show the file at all. Heres a look at my code in my html.
<form id="form" method = "POST" enctype="multipart/form-data" action="{{url('/SetUp/Save')}}" >
<div id = "box" class="container dark well dataBox textBox2">
<script type="text/javascript">
num = 1; // only show the first box and hide the others VM keeping the num var for use later
</script>
@for($i = 1; $i <= $amount; $i++)
<div id = {{"panel".$i}} class="panel panel-default">
<div class="panel-heading">
<button type="button" class="btn btn-danger left">Remove
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</button>
<h3 class="panel-title"><strong> Location {{$i}}</strong></h3>
</div>
<div class=" pBody panel-body">
<div class="row">
<div class="form-group col-lg-6">
<label class = "control-label" label-default="Address" for="Address">Address
</label>
<input type="text" name= "Address[{{$i}}]" class="form-control" placeholder="Address">
</div>
<div class="form-group top col-lg-6">
<label class = "control-label" label-default="City" for="City">City</label>
<input type="text" name = "City[{{$i}}]" class="form-control"
placeholder="City">
</div>
</div>
<div class="row">
<div class="form-group col-lg-6 ">
<label class = "control-label" label-default="Picture" for="Picture">Picture</label>
<input type="file" name = "Picture[{{$i}}]" class= "form-control" id="Picture"
placeholder="Picture">
</div>
Controller
public function saveData(PostRequest $request) {
}
Post Request
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Log;
class PostRequest extends Request
{
/**
* 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 wantsJson()
{
return true;
}
public function response(array $errors)
{
return response()->json($errors);
}
public function rules() {
$rules = [];
var_dump($this->request); // request object print out
var_dump($this->request->get('Picture'); // return NULL to console
foreach($this->request->get('Picture') as $key => $val) // error 500 meaning Picture is null.
{
$rules['Picture.'.$key] = 'required|image';
}
}
As you can see I am trying to validate the request but i wind up with an error. and when I var_dump the request I get null and its not showing up in the var_dump here is a look at the var_dump print out
object(Symfony\Component\HttpFoundation\ParameterBag)#194 (1) {
["parameters":protected]=>
array(15) {
["Address"]=>
array(5) {
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
}
["City"]=>
array(5) {
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
}
}
and here is my ajax
/////////////send data to server
$("#form").on("submit",function(event) {
console.log("Fire");
$.ajax({
type: 'POST',
url: '/SetUp/Save',
//data: $(this).serialize(), // put the data in url form // send data over
data: new FormData( this ), // send data including file input to xml to process for sever
processData: false,
contentType: false,
dataType: 'json',
success: function(data){
HolderArray = []
JsonKeys = Object.keys(data);
for(x in data){
HolderArray.push(data[x])
}
for (var i = 0; i < JsonKeys.length; i++) {
holder = JsonKeys[i].replace(".","[");
$("[name='"+holder+"]'"+"]").parent().addClass(' animated shake has-error') .append(' <span class="help-block"> <strong>'+HolderArray[i]+'</strong></span>')
}
},
error: function(data){
var errors = data.responseJSON;
console.log(errors);
// Render the errors with js ...
}
});
return false;
});
No Picture to be found. it seem like the request object is not getting file input. I have been searching online and no luck at solving this problem. Please help i am new to Laravel as well so please help me out . Thank You
Upvotes: 1
Views: 1347
Reputation: 309
aha i figured out my problem! in my Post Request class instead of calling
$this->request->get('Picture'); // this will return null
// But
$this->file('Picture');// will return the file and show that the file has been uploaded. It seems like the file does not bind the to request object.
now instead of validating my field like so
foreach($this->request->get('Picture') as $key => $val)
{
$rules['Picture.'.$key] = 'required';
} // this gives a null error
I changed that to this and it works
foreach($this->file('Picture') as $key => $val)
{
$rules['Picture.'.$key] = 'required';
}
it works !!! .
thanks for your help !!!
Upvotes: 0