Laravel Form Requests hasFile() not working - php

I'm using Laravel form request to validate a array of input group. I need to check files using a loop for a custom validation. But it shows this error.
Error
Call to undefined method Symfony\Component\HttpFoundation\ParameterBag::hasFile()
My HTML inputs (createUser.blade.php)
<input type="text" name="users[0][name]">
<input type="number" name="users[0][age]">
<input type="file" name="users[0][profile_pic]">
<input type="text" name="users[1][name]">
<input type="number" name="users[1][age]">
<input type="file" name="users[1][profile_pic]">
My Form Request (CreateUserFormRequest.php)
dd($this->request->hasFile('users');

Inside FormRequest class if you want to check whether request has value or not then you can do like below
$this->has('users')
If you want to check request has file or not then
$this->hasFile('logo')
also make sure input type is file
To check all data in request then
$this->all()
For files make sure input type is file
<input type="file" name="users[1][profile_pic]">
Also make sure enctype="multipart/form-data" in form tag
<form method="POST" action="" enctype="multipart/form-data">
Updated
<form method="POST" action="{{route("testing")}}" enctype="multipart/form-data">
#csrf
<input type="number" name="users[0][name]">
<input type="number" name="users[0][age]">
<input type="file" name="users[0][profile_pic]">
<input type="number" name="users[1][name]">
<input type="number" name="users[1][age]">
<input type="file" name="users[1][profile_pic]">
<button type="submit">Submit</button>
</form>
and in Form request
foreach ($this->users as $key=>$value){
if($value['profile_pic']!=null){
dump($value['profile_pic']);
}
}
If you are looking for file validation then you can do the following
public function rules()
{
return [
'users.*.profile_pic'=>'required|file'
];
}
and in your view
<input type="file" name="users[0][profile_pic]">
{{ $errors->first('users.0.profile_pic') }}
<input type="file" name="users[1][profile_pic]">
{{ $errors->first('users.1.profile_pic') }}

Related

Submitting an image alongside a form with one button

I have a regular form which submits to my database but I want to include a file upload in the form, so as to receive the form input in the expected table using PHP post and also receive the uploaded image.
Use <input type="file"> to include a file upload in the form.
Use something like this to send Files as well as other inputs to the processing page where you can access files and post variables.
<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="txtbox">
<br />
<input type="file" name="filebox">
<br />
<input type="submit" name="submit" value="submit">
</form>
When this is submitted you can access the data from $_FILES and $_POST
There are various input types in HTML just as the below:
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
Each of the above inputs demand different conditions and formats to be satisfied.
For your query the answer is either:
<input type="file">
For webpages that require inputs in a file format. Or,
<input type="image">
For webpages requiring input in an image format.
For further details view the below link for help:
https://www.w3schools.com/html/html_form_input_types.asp

Form not sending file when submitted with Jquery

I have this form:
<label for="pdffile">Upload</label>
<form class="form-horizontal" role="form" method="POST" name="upload" id="upload" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="file" id="pdffile" style="display:none" form="upload"
</form>
It is supposed to be submitted with this jquery:
$('#pdffile').change(function() {
$('#upload').attr("action", "/upload").submit();
});
However when I check in PHP, nothing is uploaded:
dd($request)
Always gives me back null instead of the requested item.
Any help?
Your input does not have name
<input type="file" id="pdffile" style="display:none" form="upload"
change to
<input type="file" id="pdffile" style="display:none" name="upload"/>
and in your action you can use $_FILES to get upload file

Form submission in laravel is not working and show token-mismatch Exception

i'm new laravel learner. i have create a form in laravel 5.4 and add two field one is textfield and another is submit button. i have create a name route for form submission. but when i submit the form it's show token mismatch exception. i can't find out error from my code.
Here, is my form code.
<form method="post" action="{{route('f.submit')}}">
{{csrf_field()}}
<input class="form-control" type="text" name="fname">
<input type="submit" name="submit" value="Create">
</form>
here is my route,
Route::post('formsubmit','HomeController#formSubmit')->name('f.submit');
and here my controller,
public function formSubmit()
{
echo "Form Submitted";
}
As of Laravel 5.6 you can just use the new blade directive of #csrf. In example:
<form method="POST" action="/profile">
#csrf
...
</form>
Try this.
for Form.
<form method="post" action="{{route('f.submit')}}">
{{csrf_field()}}
<input class="form-control" type="text" name="fname">
<input type="submit" name="submit" value="Create">
In Controller.
public function formSubmit(Request $request)
{
$request->all();
}
You have to add input hidden field for token. change you view as:
<form method="post" action="{{route('f.submit')}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input class="form-control" type="text" name="fname">
<input type="submit" name="submit" value="Create">
</form>
Can you please make sure your /storage directory has proper write permissions (0755) ? If you're in Linux, you can change that by sudo chmod 0755 -R storage/
Also, try not escaping the csrf_field() value. Instead of {{ csrf_field() }} try {!! csrf_field !!} since that helper function spits out HTML

Form action html to php

i have
<form action = "https://creator.zoho.com/api/xml/fileupload/scope=creatorapi&authtoken=**************" method="post" enctype="multipart/form-data">
<input type="text" name="applinkname" value="sample">
<input type="text" name="formname" value="Employee">
<input type="text" name="fieldname" value="File_upload">
<input type="text" name="recordId" value="1235667754455">
<input type="text" name="filename" value="profile.jpg" >
<input type="file" name="file">
<input type="submit" value="Update">
</form>
But i must send 2500 files, how can i transform this html to php, my problem is <input type="file" name="file">, how convert it to give a value in php ?
thanks you
You can use $_REQUEST['FILE']=$SOME VARIABLE;
And name is same what you use in your database
then apply enctype="multipart/form-data".

Tomcat 6 & PHP with enctype - No input data returned

I have this problem when I'm using PHP5/HTML on Apache-Tomcat6.
Here's an example for one of the forms I use in my site:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
Whenever I add the 'enctype' attribute to any form; neither the $_FILES['image'] is returned nor the $_POST variables. As long as the 'enctype' is not there, everything (except for the file input of course) works as expected. Can any one guide me please?
You won't be able to post data with a method of get on your form.
In test.html:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
In hello.php:
<?php
print_r($_POST);
print_r($_FILES);
Depending upon your server configuration, this will combine $_GET, $_POST, and $_COOKIE, but you'll still want to post with file inputs.
print_r($_REQUEST);

Categories