1

I have an image field that should not be required, but should be limited to possible file types But, if I validate mimes, submitting the form without an image does not work

request()->validate([
       'name' => ['required'],'name' => ['required'],
       'type' => ['required'],'master_id' => ['required'],
       'description' => ['required'],
       'whom_id' => ['required'], 'short_description' => ['required'],
       'image' => ['mimes:jpg,bmp,png','max:3072'],
        ]);
if(request()->image){
        $ext = request()->file('image')->extension();//Получение расширения файла
        $path = request()->file('image')->storeAs('image', $course->id.'mainImage.'.$ext);
        $course->image = $path;
 }

validate err

5
  • Try adding sometimes| in front, Cf. stackoverflow.com/q/44265107/367456 (may depend on how you submit the form, see as well Conditionally Adding Rules and the note therein). Commented Jul 18, 2023 at 6:24
  • @hakre Doesn't work for me ('image' => ['sometimes','mimes:jpg,bmp,png','max:3072'],) form submit with vue Commented Jul 18, 2023 at 6:32
  • If you're still in for another try, then also add nullable| in front and see if there are any changes. I've picked that from A Note On Optional Fields. Commented Jul 18, 2023 at 6:35
  • And btw. which laravel version, I paste 5.x links all the time but have no clue if it is the version you have in use. Just FYI. Commented Jul 18, 2023 at 6:36
  • @hakre Latest but not least, I'm looking into the documentation of my version for your advice. nullable worked for me, thanks! Post as an answer so I can mark it as correct? Commented Jul 18, 2023 at 6:42

1 Answer 1

2

Normally for standard form submits, the answer is sometimes, however you're submitting by Vue, so it required nullable.

    'image' => ['nullable', 'mimes:jpg,bmp,png','max:3072'],

Mind the Note On Optional Fields.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.