Laravel input file validation - laravel

I'm trying to make my validator working but for some reason it only accepts the pdf files. I want to make the validator works for all of them.
$filePathTemp = Input::file('cv');
$file = array('cv' => $filePathTemp);
$rules = array('cv' => 'mimes:doc,pdf,docx');
$validator = Validator::make($file, $rules);
Any idea why this solution isn't working?

put this code
$validator = Validator::make($request->all(), [
'file' => 'required|max:10000|mimes:doc,docx,pdf'
]);
and in config/mimes.php add the below mime types:
'doc' => array('application/msword', 'application/vnd.ms-office'),
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
Hope this may help you!

I think you can try this:
$validator = Validator::make($request->all(), [
'cv' => 'mimes:doc,pdf,docx'
]);
Hope this work for you!!!

it worked adding the txt type to the other mimes

Related

Problems with Laravel data validation

I'm having some problems with validations in my api.
I need to send a json array like this:
[
{
"acktime": "2021-09-25 08:45:07",
"temp": 15.6
},
{
"acktime": "2021-09-25 08:45:07",
"temp": 15.6
}
probably more array....
]
I would like to vaidate one by one array and store only the valid data returning error for unvalid data, I have tried a foreach cylce but it convert the array to object but the validate::make want only array.
I have tried this:
$validator = Validator::make($request->all(), [
'*.acktime' => 'required',
'*.temp' => 'required|numeric'
]);
$validatedData = $validator->validated();
var_dump($validatedData);
return response()->json($validatedData);
But If I send wrong data I get only error without having valid data, so I've tried this way:
foreach($datas as $data){
$arr = (array)$data;
$validator = Validator::make($arr, [
'acktime' => 'required',
'temp' => 'required|numeric'
]);
if ($validator->fails()) {
continue;
} else {
$newrawData = new rawData([
'acktime' => $data->acktime,
'temp' => $data->temp,
'synctime' => now()
]);
$newrawData->save();
}
}
return response('OK', 200); //or error if some data are not ok
}
In this way it work, bot I have no idea about get, a probable, validation error..(for the moment there's a continue for continue the cycle) any suggestion?
There are two ways for approaching this kind of validation:
make a custom rule in laravel validation from below and put your validation code in it and this will work:
https://laravel.com/docs/8.x/validation#custom-validation-rules
easier way:
$data = [ 'data' => $requests->all() ];
$validator = Validator::make($data, [
'data.*.name' => 'required|string',
'data.*.' => 'required|string'
]);

Laravel 5.6 validate required_without multiple

I use laravel 5.6 and I try to use validate to check my input.
But I have an issue with required_without.
I have 4 input : heure_bureau / heure_supp_bureau / heure_terrain / heure_supp_terrain
I must fill one input at least. So if I fill heure_bureau, the others aren't necessary.
So I use this code :
$validator = \Validator::make($request->all(), [
'heures_bureau' => 'nullable|date_format:"H\hi"|required_without:heures_supp_bureau,heures_terrain,heures_supp_terrain|before:07h45',
'heures_supp_bureau' => 'nullable|date_format:"H\hi"|required_without:heures_bureau,heures_terrain,heures_supp_terrain|before:13h15',
'heures_terrain' => 'nullable|date_format:"H\hi"|required_without:heures_bureau,heures_supp_bureau,heures_supp_terrain|before:07h45',
'heures_supp_terrain' => 'nullable|date_format:"H\hi"|required_without:heures_bureau,heures_supp_bureau,heures_terrain|before:13h15'
], $messages);
But it doesn't work. I have an error for each other input when I fill one.
If I use requried_without with only one input, it work well but not when I use it with multiple inputs.
Where am I wrong ?
Thank for your help !
try this:
$validator = \Validator::make($request->all(), [
'heures_bureau' => 'nullable|date_format:"H\hi"|required_without_all:heures_supp_bureau,heures_terrain,heures_supp_terrain|before:07h45',
'heures_supp_bureau' => 'nullable|date_format:"H\hi"|required_without_all:heures_bureau,heures_terrain,heures_supp_terrain|before:13h15',
'heures_terrain' => 'nullable|date_format:"H\hi"|required_without_all:heures_bureau,heures_supp_bureau,heures_supp_terrain|before:07h45',
'heures_supp_terrain' => 'nullable|date_format:"H\hi"|required_without_all:heures_bureau,heures_supp_bureau,heures_terrain|before:13h15'
], $messages);

Laravel, Forcing A Unique Rule To Ignore A Given ID

I am following the instructions from the Laravel Documentation but I can't seem to make the rule work. Basically, I want to add a unique rule for both email and username fields for updating the user profile, but it doesn't seem to work. Am I missing something?
Here is the function for updating user credentials
public function updateCredentials(Request $request, User $user)
{
Validator::make($request->all(), [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'username' => [
'required','string','min:4','max:255',
Rule::unique('users')->ignore($user->id),
],
'email' => [
'required','string','email','max:255',
Rule::unique('users')->ignore($user->id),
],
'asdf' => 'required',
'type' => 'required',
])->validate();
}
Thanks in Advance!
If you want to update user profile,without changing his id,you dont need to set specific rule for that.
Without explicitly setting the new user id,it should stay the same.
You can just name the data you want to change like this:
$user->name = request('name');
$user->lastname = request ('lastname);
$user->email = request('email');
...
$user->save();
session()->flash('message','Profile update complete!');
return redirect('/home');
Doing this the data you want will update,but the id will stay the same.
Seems to be some bug.
Adding filed name worked for me:
Rule::unique('users')->ignore($user->id, 'users')

Laravel:file extension validation

I am using laravel 5.2 ,i use image rule to validate images uploaded by user,which requires php_fileinfo extension to be installed ,but is there a way to validate images only for there extensions like .png,.jpg etc?
Use mimes
'photo' => 'mimes:jpeg,bmp,png'
You can add the mime type to your validation like you would add any other rule:
$rules = [
'image' => 'required|image|mimes:gif,png'
];
Source: https://laravel.com/docs/5.5/validation#rule-mimes
yes you can by using mimes validation rule
Try this.
$validator = Validator::make(
[
'file' => $request->file,
'extension' => strtolower($request->file>getClientOriginalExtension()),
],
[
'file' => 'required',
'extension' =>'required|in:txt',
]
);

Validating Matching Strings

When I use the Validation feature in Laravel, how can I add a pre-defined strings that are allowed in an Input?
For example, let's say I want the Input to contain only one of the following: foo,bar,baz, how can I do that?
$validator = Validator::make($credentials, [
'profile' => 'required|max:255', // Here I want Predefined allowed values for it
]);
Best to use the 'in' validation rule like this:
$validator = Validator::make($credentials, [
'profile' => ["required" , "max:255", "in:foo,bar,baz"]
]);
It is recommended in Laravel docs to put the validation rules in an array when they get bigger and I thought 3 rules were sufficient. I think it makes it for a more readable content but you do not have to. I added the regex portion below and I have it works. I am not that great with regexing stuff. Let me know.
$validator = Validator::make($credentials, [
'profile' => ["required" , "max:255", "regex:(foo|bar|baz)"]
]);
Works for me in Laravel 9:
use Illuminate\Validation\Rule;
 
Validator::make($data, [
'toppings' => [
'required',
Rule::notIn(['sprinkles', 'cherries']),
],
]);
Docs: https://laravel.com/docs/9.x/validation#rule-not-in
For the opposite you could use Rule::in(['foo', 'bar', 'baz'])

Resources