Laravel - mimes:pdf not working - php

I have a form with an input which allows to upload PDFs.
Here is the method that validates my input :
protected function validator(array $data)
{
return Validator::make($data, [
'title' => 'required|max:255|string',
'artist' => 'required',
'notes' => 'nullable|string',
'file' => 'required|mimes:pdf|max:500',
'g-recaptcha-response' => 'required|captcha',
]);
}
The rules are working for all the other fields, but it does not work for the file input. Whatever I upload, PDF or not, I have the following error :
The file must be a file of type: pdf.

I just solved my issue.
In case someone has the same problem, you need to set the enctype attribute of your form to multipart/form-data.
In short, something like this :
<form action="#" enctype="multipart/form-data">
...
</form>
The error is not very clear, so I hope this helps.

Related

Method Illuminate\Validation\Validator::validateFiles does not exist. Laravel 5.8 submit form

This is most likely a duplicate question but I have been looking for solutions and can't seem to find one that fixes the issue I have. I have created a function called validateRequest which validates all the fields. This function is then called in store function $post = Post::create($this->validateRequest()); I have made sure in the HTML form enctype="multipart/form-data" has been included, but every time a submit a new entry that errors appear. Not sure if I am using the return tap method correctly or something else, really appreciate some help thanks.
public function validateRequest()
{
return tap(
$validated = request()->validate([
'title' => 'required|string',
'h1' => 'required|string',
'page_title' => 'required|string',
'meta_description' => 'required|string',
'image' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
'content' => 'required|string',
'active' => 'integer'
]), function () {
if (request()->hasFile('image')){
request()->validate([
'image' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
]);
}
// Check if active is ticked
$validated['active'] = isset(request()->active[0]) ? 1 : 0;
// Create slug from title
$validated['slug'] = Str::slug(request()['title'], '-');
});
}
"files" is not a valid validator, use file without "s"
'image' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
Hope this helps

Laravel Validator. Unable to retrieve both errors and Input

I am using Laravel for a project and i am trying to validate some input fields from a form. I am using the Validator class.
Here is the code in my controller.
$validator = Validator::make($request->all(), [
'arithmos_kinhths' => 'required',
'kathgoria_kinhths' => ['required',Rule::notIn(['-'])],
'prohgoumenos_paroxos_kinhths' => ['required',Rule::notIn(['-'])],
'programma_kinhths' => ['required',Rule::notIn(['-'])],
'project_kinhths' => ['required',Rule::notIn(['-'])],
'kathogoria_epidothshs_kinhths' =>['required',Rule::notIn(['-'])],
'talk_to_eu_kinhths' => ['required',Rule::notIn(['-'])],
'pagio_kinhths' => 'required',
'sms_kinhths' => ['required',Rule::notIn(['-'])],
'internet_kinhths' => ['required',Rule::notIn(['-'])],
'international_kinhths' => ['required',Rule::notIn(['-'])],
'twin_sim_kinhths' => ['required',Rule::notIn(['-'])],
'wind_unlimited_kinhths' => ['required',Rule::notIn(['-'])],
]);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
In the blade file i am trying to catch the errors using the code bellow.
#if($errors->any())
#foreach($errors->all() as $error)
<script>
$.notify(
{
title: '<strong>ERROR!</strong>',
message: '{{$error}}',
},
{
type: 'danger',
},
)
</script>
#endforeach
#endif
Also i want to put the old values into the input fields using {{old('value'}}
The problem i have is that i can't combine both errors and inputs. If i return only the errors using withErrors($validator) the errors are printed out. And if i return only withInput i have the post values.
Any ideas?
withInputs()
try this, i hope that help a little , or
you can
return back()->with('errors',$validator)->withInputs()
you can use $this->validatorWith([]) then follow your code, you don't need to manually redirect back to that page. your request will auto redirect to that page from where request has happened. this function belongs to trait Illuminate/Foundation/Validation/ValidatesRequests which is use by app\Http\Controllers\Controller.php. you just need to use. for more about this trait see here ValidateRequest
$this->validatorWith([
'request_param' => 'required',
]);

Laravel old POST data empty after validation

I am printing the contents of old() in my view:
{{ print_r(old('steps'), true) }}
When I submit the form with the following validation rules, the old data prints fine:
$this->validate($request, [
'steps.*.name' => 'required',
]);
When I add more rules, the old data dissapears completely:
$this->validate($request, [
'steps.*.name' => 'required',
'steps.*.title' => 'required',
'steps.*.type' => 'required',
'steps.*.answer_options' => 'nullable|required_if:steps.*.type,Question',
'steps.*.input_type' => 'nullable|required_if:steps.*.type,Input',
]);
I've confirmed this only happens AFTER validation. How do I fix this?
Try to set SESSION_DRIVER=file to get it work
See related

Laravel 5.5 "The file must be an image" on image array

I am working on a Laravel 5.5 project which is giving me some issues... One of them is that when I upload some image it returns me an error: "The file must be an image" with the following code in the controller.`
public function update(\App\Property $property, Request $request)
{
$rules = [
'images.*' => 'mimes:image|max:2048',
'zone' => 'required'
];
$messages = [
'images.mimes' => 'The file must be an image', // in my file that is a translated of the original message
'images.max' => 'The images sizes must be under 2MB',
];
$validator = Validator::make($request->all(), $rules, $messages);
if($validator->fails()) {
parent::message(
$validator->errors(),
'¡Error!',
'error'
);
return back()->withInput();
}
Just in case:
I was reading some people who had the same issue but in my case I have the usual <form method="POST action="..." enctype="multipart/form-data">
I made a dd($request->files) on the request but the all the images I upload seems to be images.
Also I've tried to use the iteration method as following:
if($request->hasFile('images')) {
foreach ($images as $image)
{
$validator = Validator::make(
[ 'image' => $image ],
[ 'image' => 'mimes:jpeg,png,jpg,gif,svg|max:2048' ],
[
'image.mimes' => 'Images must have format (JPEG, PNG, JPG, GIF or SVG)',
'image.max' => 'Each image must be under 2MB'
]);
if($validator->fails()) {
parent::message(
$validator->errors(),
'¡Error!',
'error'
);
return back()->withInput();
}
}
}
But when the images are too bigger than 2MB the request is not even passing through if($request->hasFile() function.
And I want a generic error for all the images and not validate every image, is that possible? By the way in the Laravel Documentation it hasn't the previous iterating method.
I was trying to figure this out as well and it seems symfony has been updated since Laravel 5.5. On Laravel 8 I can tell you that the following combination of settings fixed the error I had yielding the same error type you had:
In your form:
make sure your form has
<form method="POST action="..." enctype="multipart/form-data">
In your validator:
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
In the event that you are using Laravel Collectives for your form, then you can implement the enctype as show below :
{!! Form::open([‘action’ =>’ProfilesController#update’,
‘method’ => ‘POST’, ‘enctype’ => ‘multipart/form-data’]) !!}

Validation doesn't work for image upload in laravel 5

I have problem when validating inputs. All input fields pass the validation process except image fields.
This is my file upload code in html:
<div class="control-group">
<label class="control-label" for="fileInput"> Cover picture: </label>
<div class="controls">
{!! Form::file('cover') !!}
</div>
</div>
And how I get data from view in controller:
$datas = array(
'name' => Input::get('name'),
'color' => Input::get('color'),
'size' => Input::get('size'),
'cover' => array('cover' => Input::file('cover'))
);
And this is rules:
$rules = array(
'name' => 'required',
'color' => 'required',
'size' => 'required',
'cover' => 'required|mimes:jpeg,jpg,png|max:10000'
);
And Validation facades`s make method:
$validator = Validator::make($datas, $rules);
As I mentioned earlier, all validation rules passed for input, but for image it gives me an error:
The cover must be a file of type: jpeg, jpg, png.
Now, how can I fix it?
I think you should approach this a little differently...
If you instead just create your function like so..
// The Illumniate/Http/Request version of Request in laravel
public function yourFunction(Request $request){
....
$rules = ...;
$validator = Validator::make($request->all(), $rules);
}
The expected format of the Validator is handled automatically because it returns it. And when it runs through you can handle everything the same you already have.
Since this is accepted. The way to actually fix it with the code above is to just remove the multi-dimensional array and just use Input::file('cover') as it returns an array on its own.

Categories