1

my problem : i am using laravel framework php

i want validate a field witch my field sometime is File(image) and sometime is String(src of same image)

is there any shortcut or solution in Laravel ???

4 Answers4

4

The specifics of your question are not exactly clear but I think you can manage what you want by doing something like:

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

]);

Where UploadedFile refers to \Illuminate\Http\UploadedFile and stringOrFile refer to the name of the input your are validating.

However if possible use a different name for the input depending on what it is so you can have more complex validation rules on each different input.

More details on these kinds of validation rules in the manual

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • i have form in client that send me multi field that 2 field of its form is photo i want edit photo of my object in my program , and then i have one rule that when client wont update one of photo it mean that this photo already updated and it just must send me SRC of its photo and then i want check that what he had send me SRC or File , just it – Fazel Saeedi Feb 11 '21 at 20:58
2

Or you can do something like that in your request class ;)

/**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'image' =>$this->getValidationRule('image'),
        ];
    }

    /**
     * @param String $key
     * @return string
     */
    public function getValidationRule(String $key): string
    {
        if (request()->hasFile($key)) {
            return "nullable|mimes:png,jpg";
        }
        return "nullable|string";
    }
1

Actually while using the form data, empty image is received as a string "null". you can manage this in your controller.

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

or you can send an empty string in case if image is not attached.

Muhammad
  • 339
  • 3
  • 22
0

I use this code example. take any part useful for you.

$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);

$msg for errors

you should create error page blade name error contains

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

///

in blade that contain form for validation write top code

@include('error')

include error page if exist any error to display errors

Waad Mawlood
  • 727
  • 6
  • 10