T
T
Terroris3372021-05-04 11:31:01
Laravel
Terroris337, 2021-05-04 11:31:01

How to formulate a validation rule when updating a user's avatar?

Good day, I ran into an image validation problem when updating user data.
There is a set of validation rules:

public function rules()
    {
        return [
            'fname' => 'required|alpha|string|max:255',
            'lname' => 'required|alpha|string|max:255',
            'pname' => 'required|alpha|string|max:255',
            'avatar' => 'nullable|image|mimes:jpeg,png',
            'birth_date' => 'required|date',
            'login' => "required|unique:employees,login,{$this->id}",
            'password' => 'nullable|min:10|alpha_dash',
            'role' => 'required',
            'phones' => 'required|array',
            'phones.*.phone' => 'required|numeric',
            'phones.*.is_personal' => 'nullable|boolean',
            'phones.*.is_work' => 'nullable|boolean',
            'emails' => 'required|array',
            'emails.*.email' => 'required|email',
        ];
    }


The same FormRequest is used to validate the creation and update of user data. The problem is that the user's avatar can be updated (a new file will arrive on the server and everything will work), or it can remain current (the avatar field will not arrive at the server), and validation is waiting for the file to arrive.
Tell me how to correctly write vilidation rules for creating and updating an image.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-05-04
@Terroris337

https://laravel.com/docs/8.x/validation#validating...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question