ellajhonm's avatar

.png file not passing validation rule 'image' and 'mimes:jpg,png,jpeg,gif,svg'

I initially put an image rule, but my .png file wouldn't pass, I then tried the mimes:jpg,png,jpeg,gif,svg but it still won't work. It still keeps displaying "Image must be image" or "Image must be jpg,png,jpeg,gif,svg"

EDIT: I tried dd(request()->file('image')->getClientOriginalExtension()); on the file and it returns "Call to a member function getClientOriginalExtension() on null", so it's not reading any file? How is this possible?

Controller:

 public function register(){
        $validatedAttributes = request()->validate([
            'name' => ['required','max:100',new LetterSpaceOnly],
            'contact' => ['required', 'digits:11','unique:clients,contact'],
            'email' => ['required', 'email', 'max:255', 'unique:clients,email'],
            'password' => ['required','string',Password::min(8),'confirmed'],
            'address' => ['required','min:5','max:250','string'],
            'image' => ['nullable','mimes:jpg,png,jpeg,gif,svg']
        ]);

        // if may image
        if (isset($validatedAttributes['image'])){
            $path = request()->file('image')->store('public/storage/images','s3');
         //    ignore url error, intellisense issue
            $validatedAttributes['image'] = basename($path);
            $newClient = Client::create($validatedAttributes);
        }
        else{
            // if wala
            $newClient = Client::create([
            'name' => request()->input('name'),
            'contact_num' => request()->input('contact_num'),
            'email' => request()->input('email'),
            'password' => request()->input('password'),
            'address' => request()->input('address')
            ]);
        }

                // log in the user after account creation
                Auth::login($newClient);
            
                return redirect('/');


    }

Input tag:

<div class="row row-space">

                        <div class="input-group">
                            <label class="label" style="color: black;">Company Logo</label>
                            <input class="input--style-4" type="file" name="image" accept="image/*">
                                @error('image')
                                <div class="text-danger">{{ $message }}</div>
                            @enderror

                    </div>

Link to the picture that I'm trying to upload: https://drive.google.com/file/d/1HdEkUA50OoLofpkrmAv1ymLwPKMAMpc2/view?usp=sharing

0 likes
1 reply
ellajhonm's avatar
ellajhonm
OP
Best Answer
Level 2

Silly me, forgot to add enctype="multipart/form-data" in the <form> for file upload ^^; XD

Please or to participate in this conversation.