I'm trying to add some 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 file is not uploaded or changed or whatever. So, 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:
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, adding additional rules to an attribute via sometimes
doesn't work that way, these added rules are not being processed by a validator.
I'd like to ask, is there any way to validate these rules without revalidating the whole thing manually?
via Eternal1