0
votes

I'm trying to add custom validation logic for file uploads for my admin panel. Right now my file fields can return either Illuminate\Http\UploadedFile or string|null if the file is not uploaded or changed or whatever. What I'm doing is, I created a custom rule that looks like this:

'image' => [
    'required',
    'admin_file:mimes:jpeg;png,dimensions:min_width=800;min_height=600'
]

I then parse all the arguments I pass, and the thing is, I naturally want all of them applied only if my value is an instance of UploadedFile. I use the following code for my custom validation:

<?php

class AdminFileValidator
{
    public function validate($attribute, $value, $parameters, Validator $validator)
    {
        $rules = implode(
            "|",
            array_map(function($item) {
                return str_replace(";", ",", $item);
            }, $parameters)
        );

        $validator->sometimes($attribute, $rules, function() use ($value) {
            return $value instanceof UploadedFile;
        });

        return true;
    }
}

The problem is with adding additional rules to an attribute via sometimes doesn't work that way. The added rules are not being processed by a validator.

Is there any way to validate these rules without revalidating the whole thing manually?

1
It honestly makes no sense why you would want to create a custom validator for this. Laravel comes with file rule to check if the file was successfully uploaded. There are also rules to make it optional. - Sandeesh
There are many different types of files that i need to validate. Like documents, images, images+pdf, and others. And mime rule only accepts UploadedFile instances, and fails if i the string is being passed (which i cannot reasonably avoid, because everything is wrapped into FormRequest) - Eternal1
So you want a string value to be successfully processed instead of throwing an error? As far as i see, the rules file, mimetypes, mimes along with sometimes does exactly what you need except for string allowance. I'm not sure how'd you get a string in a form file input. - Sandeesh
@Sandeesh, I assume that there are two different forms that could be used. 1. Where the user can select an image form PC 2. Where user can select a url or image path.. Both of them pointing to same request - manix
@manix then wouldn't it be easier to have a url input and add a required_unless rule. - Sandeesh

1 Answers

0
votes

What I see is that your are using sometimes inside of a rule. From my perspective you need to take it out, even better without use a custom class.

Using Validator object:

$validator = Validator::make($data, [
    'image' => 'required',
]);

$validator->sometimes('image', 'mimes:jpeg;png,dimensions:min_width=800', function($value) {
    return $value instanceof UploadedFile;
});

If you are using a Request class you could override the function getValidatorInstance in order apply the conditional rules:

protected function getValidatorInstance(){
    $validator = parent::getValidatorInstance();

    $validator->sometimes('image', 'mimes:jpeg;png,dimensions:min_width=800', function($value) {
        return $value instanceof UploadedFile;
    });

    return $validator;
}