is there any way to validate a field that sometime is File(image) && sometime is String(Src of same image)



PHP Snippet 1:

$request->validate([
   'stringOrFile' => [
        function ($attribute, $value, $fail) {
            if (!is_string($value) && !($value instanceof UploadedFile)) {
                $fail('The '.$attribute.' must either be a string or file.');
            }
        }
 
   ]

]);

PHP Snippet 2:

$request->request->add(['image' => $request->image==  "null" ? null : $request->image]);
$request->validate(['image'=> nullable|sometimes|image|mimes:jpeg,png,jpg,svg])

PHP Snippet 3:

$msg=[
        'name.required'=>'<span class="badge">Name</span> is Required',
        'username.required'=>'<span class="badge">User Name</span> is Required',
        'born.required'=>'<span class="badge">User Name</span> is Required'
    ];


    $request->validate([
        'name'=>'required|string|min:2|max:30',
        'username'=>'required|string|min:3|max:20',
        'born'=>'required|date',
        'image'=>'nullable|image|mimes:jpg,jpeg,bmp,png'
    ],$msg);

PHP Snippet 4:

@if($errors->any())
{!! implode('', $errors->all('<div class="alert alert-danger">:message</div>')) !!}
@endif

PHP Snippet 5:

@include('error')