Reputation: 1073
I am trying to create a custom validation to check if a file was uploaded in a form.
I looked at the custom errors docs but couldn't make it work.
I found this tutorial: Custom Error
In my controller I do this:
if($request->hasFile('file'))
$has_file = 1;
else
$has_file = 0;
$this->validate($request, ['file' => 'isuploaded']);
In App/ServiceProvider I added this:
Validator::extend('isuploaded', function($attribute, $value, $parameters, $validator) {
return ?;
});
What should I return here? How do I send the $has_file to the validator?
Upvotes: 0
Views: 604
Reputation: 7618
If the file
field is required the validate
will check it for you.
Assuming that your form file input has name="file"
, just:
$this->validate($request, [
'file' => 'required
]);
In this way if there is no file the validation will fail.
If this is not your case and you want to create the custom rules in Validator::extend
you should write your own business logic to check if the file exists and just return true/false
.
Upvotes: 1