Neeraj1005's avatar

Validation Error: The logo must be an image.

In my project, when I updating the other fields the image upload validation makes a problem. Basically, I want simply update the input values and the Image is updated when if the file is selected but I write the condition but it not working.

Can anyone please look check my code and tells me what I did wrong? This is update method

 public function logo_update($id)
    {
        $validatedData = $this->validate([
            'name' => 'required',
            'job_title' => 'required',
            'facebook_url' => 'required',
            'linkedin_url' => 'required',
            'insta_url' => 'required',
            'twitter_url' => 'required',
            'logo' => 'image|max:512|mimes:png,jpg,jpeg',
        ]);

        $row_data = ThemeTeam::find($id);

        if ($this->logo) {
            $validatedData['logo'] = $this->logo->storePublicly('client-logo','public');
        }

        $row_data->update($validatedData);

        session()->flash('message', 'Client Logo updated successfully');

        $this->iteration++;

        $this->reset();
    }

and this is blade file

<div class="card">
    @if(!$table_not_found)
        <div class="card-header text-primary font-weight-bold">Teams</div>
        <div class="card-body">
            @if($wants_to_update)
                <form wire:submit.prevent="logo_update({{ $rowId }})">
                @else
                    <form wire:submit.prevent="store">
            @endif
            <div class="row">
                <div class="col-12">
                    <div class="row">
                        <div class="col-4">
                            <div class="form-group">
                                <label for="name">Name</label>
                                <input wire:model="name" type="text" class="form-control" id="name"
                                    aria-describedby="emailHelp">
                                @error('name')
                                    <p class="text-danger">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="form-group">
                                <label for="job_title">Job Title</label>
                                <input wire:model="job_title" type="text" class="form-control" id="job_title"
                                    aria-describedby="emailHelp">
                                @error('job_title')
                                    <p class="text-danger">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="form-group">
                                <label for="facebook_url">Facebook Url</label>
                                <input wire:model="facebook_url" type="text" class="form-control" id="facebook_url"
                                    aria-describedby="emailHelp">
                                @error('facebook_url')
                                    <p class="text-danger">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="form-group">
                                <label for="linkedin_url">LinkedIn Url</label>
                                <input wire:model="linkedin_url" type="text" class="form-control" id="linkedin_url"
                                    aria-describedby="emailHelp">
                                @error('linkedin_url')
                                    <p class="text-danger">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="form-group">
                                <label for="twitter_url">Twitter Url</label>
                                <input wire:model="twitter_url" type="text" class="form-control" id="twitter_url"
                                    aria-describedby="emailHelp">
                                @error('twitter_url')
                                    <p class="text-danger">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="form-group">
                                <label for="insta_url">Instagram Url</label>
                                <input wire:model="insta_url" type="text" class="form-control" id="insta_url"
                                    aria-describedby="emailHelp">
                                @error('insta_url')
                                    <p class="text-danger">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="form-group">
                                <label for="logo">Logo</label>
                                <input wire:model="logo" type="file" class="form-control-file"
                                    id="logo{{ $iteration }}">
                                <small id="emailHelp" class="form-text text-muted">JPEG, PNG
                                    only</small>
                                @error('logo')
                                    <p class="text-danger">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                        @if($previous_image)
                            <div class="col-4">
                                <img src="{{ asset('storage/'.$this->previous_image) }}"
                                    width="75%" height="75%">
                            </div>
                        @endif
                    </div>
                </div>
                <div class="col-12">
                    <div class="text-right">
                        <button wire:click="resetAll" type="submit" class="btn btn-outline-secondary">
                            Cancel
                        </button>
                        <button type="submit" class="btn btn-primary">
                            {{ ($wants_to_update) ? 'Update' : 'Save' }}
                        </button>
                    </div>
                </div>
            </div>
            </form>
        </div>
    @endif
</div>
0 likes
20 replies
Tray2's avatar

Yes

'logo' => 'image|max:512|nullable',

But I prefer to have this order instead

'logo' => 'nullable|image|max:512',

But that is just my preference.

2 likes
Sinnbeck's avatar

Agreed. Makes it faster to spot nullables at a glance :)

Sinnbeck's avatar

Just a tip. As far as I know the validation rule image is just an alias for the following mimes

'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp'

So use one of them, not both

1 like
Tray2's avatar

Stealing my thunder again I see ;p

Sinnbeck's avatar

@tray2 Sorry. I will let you get to 300 now :D

@neeraj1005 That should be enough yes :) And nullable as @tray2 suggested to allow updating without uploading a new image each time.

Neeraj1005's avatar

@sinnbeck @tray2 basically I want to update image if file is selected...if it is not then only previous image still exists.

   if ($this->logo) {
            $validatedData['logo'] = $this->logo->storePublicly('client-logo','public');
        }
Neeraj1005's avatar

@sinnbeck No... using nullable image colum is updated in null if i did not select an image.

Tray2's avatar

Since you pull the record from the database you only need to update it if a file has been provided

if ($request->hasFile('logo')) {
    //Do the stuff here to update the image
}

If it has no image provided it will use the one you alredy got.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You can also easily unset it :)

if ($this->logo) {
            $validatedData['logo'] = $this->logo->storePublicly('client-logo','public');
        } else {
      unset($validatedData['logo']);
}
1 like
Neeraj1005's avatar

@sinnbeck yes this one is working...

if ($this->logo) {
            $validatedData['logo'] = $this->logo->storePublicly('client-logo','public');
        } else {
      unset($validatedData['logo']);
}

can you please tell me why we used this `unset($validatedData['logo']); and why this is not working...?

if ($this->logo) {
            $validatedData['logo'] = $this->logo->storePublicly('client-logo','public');
        }
Sinnbeck's avatar

In you validation you are allowing a null value.. But that means that validatedData['logo'] is actually set to null.. And when you save, this value gets saved as well.

Neeraj1005's avatar

@sinnbeck if I did not set nullable in validation condition or use this if condition

if ($this->logo) {
            $validatedData['logo'] = $this->logo->storePublicly('client-logo','public');
        }

Then why this send me an validation error....? If I used this same technique in laravel controller then it normally passed but in livewire this send me an validation message...

Sinnbeck's avatar

Actually you could give sometimes a try instead. I am unsure, but it might autoclean

'logo' => 'sometimes|image|max:512',
Neeraj1005's avatar

@sinnbeck okk i try this one also... please can you clear my doubt in this

if I did not set nullable in validation condition or use this if condition. Then why this send me an validation error....?

Sinnbeck's avatar

Because it expects an image and it gets none :) You tell the validator "This input should be an image!"

1 like

Please or to participate in this conversation.