Laravel validation pdf mime - validation

I have tried below mime types for validating PDF files.but none of them doesnt pass the validation .
$rules = [
....
"file" => "required|mimes:application/pdf, application/x-pdf,application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf|max:10000"
....
]

Just add file extension
$rules = [
"file" => "required|mimes:pdf|max:10000"
]
From laravel Docs:
Even though you only need to specify the extensions, this rule
actually validates against the MIME type of the file by reading the
file's contents and guessing its MIME type.
Update:
Starting from Laravel 5.2, you could validate file upload against full MIME type, new validation rule called: mimetypes.
$rules = [
"file" => "required|mimetypes:application/pdf|max:10000"
]

Related

Laravel Predefined options in form request

I've created a FormRequest to validate some fields, and I would like that one of those fields only accept the options that a I give it
Searching I found something like this
"rule" => 'required|in:Option1,Option2,Option3',
This accept only the predifined options, but in the error message only shows
"The rule type is invalid."
And I would like that too shows the valid options predefined in the rules.
How could i do this?
In the end I modified my rule to this:
"rule" => ['required', 'in:Option1,Option2,Option3']
and I customized the error message with the following
public function messages()
{
return [
'rule.in' => 'The rule field must be Option1, Option2 or Option3.'
];
}

Laravel validate request filter field with dot (period) in field name

Here is to validate form request in laravel, request contains filter and field name in the filter has period(dot) present.
Sample Request url
...?filter[entity.abc][]='value'
Here entity.abc is actually a string, but laravel considers it to be array of object, when rule is given for 'filter.entity.abc'
filter:[
[entity]: [ {abc:'value'}]
]
which is actually
filter:[
[entity.abc]:['value']
]
So we need to make regex for second dot, which equivalents to:
public function rules()
{
return [
'filter.entity\.abc' => ['bail', 'sometimes', 'array'],
'filter.entity\.abc' => ['uuid']
];
}
Above always retuns true,even when invalid uuid is present
why not modify your request like this?
...?filter[entity][abc][]='value'
Edit:
You can use custom validation in laravel where you can deconstruct the parameters and check the values manually
Laravel Custom Validation
https://laravel.com/docs/8.x/validation

Laravel form-data image and text fields validation

//TestRequest.php
public function rules()
{
return [
'name' => 'string|required|min:5',
'tip' => 'string|required|min:5',
'answer' => 'string|required',
'image' => 'file|required|mimes:png,jpg,jpeg'
];
}
//TestController.php
public function put(TestRequest $request)
{
$validated = $request->validated();
}
I'm doing some rest API. I need a form with some text fields and one image upload field but I have a problem with validating it.
When I'm sending the request as 'form-data' in the Postman, Laravel doesn't see in the validation any fields (why?).
When I'm sending the request as application/x-www-form-urlencoded Laravel sees my text fields, but I can't, of course, upload the image.
API will be used by the android APP. How I can solve this? How I can have both validation on text and file inputs?
Using application/x-www-form-urlencoded is the correct way to upload images. According to your second screenshot, you are not sending the file field in Postman, but you are sending it in the first screenshot.
see this
'name' => 'string|required|min:5',
minimum is 5 character but you send test or 4 chars. Laravel validation rule, if it failed it will stop or next validation will not checked.
I think I've found a solution.
Changing method from PUT to POST seems to fix this issue.

Laravel batch file upload

I have a csv file with the following structure:
id,title,sub_title,filename
1,Title 1, Sub Title 1,filename_1.mp3
2,Title 2, Sub Title 2,filename_2.mp3
3,Title 3, Sub Title 3,filename_3.mp3
(...)
I'm loading the CSV file inside a App\Console\Command (artisan command).
Assuming the files exist on the filesystem and the path is correct, for each csv line how can I upload the related file and validate it using the Validator class?
I'm using this code:
$validator = Validator::make(array('filename' => 'path_to_filename_x.mp3'), [
'filename' => 'required|file|audio:mp3,wav,ogg',
]);
if ($validator->fails()) {
echo '<pre>';
print_r($validator->errors());
echo '</pre>';
die();
}
I'm getting the error:
"The attribute must be a file."
Because I'm sending text instead of a resource (i suppose).
How can I upload these files without using a form using Laravel 5.6?
Thks!
Question: how can I upload the related file and validate it using the Validator class?
Validating the files:
Create a custom rule in laravel and there you can add logic like.
if(file_exists($path)){
//check for file type
}
Upload the files :
use $fcontent = file_get_contents($filepath) and file_put_contents($newfilepath, $fcontent)

Laravel 5.3 : txt or csv file throws an exception (getimagesize(): Read error!)

I have used following code to validate the image file. I want to upload image file only.
However when user upload the txt or csv file it throws an exception (getimagesize(): Read error!).
Below is the validation code.
$rules = [ 'mobile_image'=>'mimes:jpg,jpeg,gif,png|dimensions:width=710,height=400', 'web_image'=>'mimes:jpg,jpeg,gif,png|dimensions:width=1182,height=300', ];
$validator = Validator::make($validateData, $rules);
if($validator->fails()){
}
I have checked in Laravel validation file.
They have used getimagesize for all file uploads.
Is there any other validation rule for txt or csv file ?
Is there any way to overwrite the validator class with custom rules ?
You're using dimensions rule, so Laravel executes getimagesize() to get image dimensions. If you want to upload text files, change mimes and remove dimensions rule.
https://laravel.com/docs/5.3/validation
Try below example code:
$validator = Validator::make($validateData,[
'mobile_image'=>'mimes:jpg,jpeg,gif,png|dimensions:width=710,height=400',
'web_image'=>'mimes:jpg,jpeg,gif,png|dimensions:width=1182,height=300',
])->validate();
UPDATE
You can try below snippet at own your risk:
$image_size = #getimagesize( $image_url );
if($image_size === false){
//Handle error message
}
else {
$validator = Validator::make($validateData,[
'mobile_image'=>'mimes:jpg,jpeg,gif,png|dimensions:width=710,height=400',
'web_image'=>'mimes:jpg,jpeg,gif,png|dimensions:width=1182,height=300',
])->validate();
}
Hope this will solved your issue.

Resources