0

I have validation:

public function saveUser($request)
    {
        // validacja
        $this->validate($request, [
            'name' => 'required|string',
            'surname' => 'required|string',
            'email' => 'required|email'
        ]);

        if ($request->hasFile('userPicture')) {
            $this->validate($request, [
                'userPicture' => 'image|max:1000'
            ]);
        }

        // save
    }

I need add to this validation:

'userPicture' => 'image|max:1000'
  • file type: only jpg/jpeg

How can I do this ?

3 Answers 3

1

What about:

'userPicture' => 'max:1000|mime:image/jpeg'

The documentation states you can use the following mime types: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

image/jpeg allows for: image/jpeg - jpeg jpg jpe

For more information: https://laravel.com/docs/5.8/validation#rule-mimes

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

Comments

0

Depending on your Laravel version there are some different ways.

https://laravel.com/docs/5.8/validation#rule-image https://laravel.com/docs/5.8/validation#rule-dimensions

Comments

0

Mime is a filetype rule that you need to set.

So, you can first make the rules array and then validate:

public function saveUser($request)
{
    // validacja
    $rules = [
        'name' => 'required|string',
        'surname' => 'required|string',
        'email' => 'required|email'
    ];

    if ($request->hasFile('userPicture')) {
        $rules['userPicture'] = 'image|max:1000|mimes:jpeg'
    }

    $this->validate($request, $rules);
    // save
}

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.