Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmytroshved's avatar

How to validate image path in editing form? Livewire 3

When I am creating recipe I have image from user

While creating I have this validation logic for image:

#[Validate]
    #[Rule(['nullable','mimes:jpeg,png,webp'])]
    public $image;

(its a file, so it works well)

But when I wanna to edit recipe image I have a problem, because in this case I am getting image like a string (path to that image) recipes-images/default/default_photo.png

How can I validate image when it is a string path?

1 like
5 replies
vincent15000's avatar

When I edit a model having an image, I don't care about the image, unless the image is replaced by another one.

1 like
dmytroshved's avatar

@vincent15000 What do you think about this code?

public function getRules()
    {
        $rules = [
            'name' => ['required', 'string', 'max:255', \Illuminate\Validation\Rule::unique('recipes')->ignore($this->id)],
            'image' => 'nullable|mimes:jpeg,png,webp|max:2048',
        ];

        if ($this->id && !$this->image instanceof \Illuminate\Http\UploadedFile) {
            unset($rules['image']);
        }

        return $rules;
    }
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'unique:recipes,name', 'max:255'],
            'description' => ['nullable', 'string', 'max:255'],
            'category' => ['required'],
            'cuisine' => ['required'],
            'menu' => ['nullable'],
            'cook_time' => ['required', 'date_format:H:i', 'not_in:00:00'],
            'servings' => ['required', 'integer', 'min:1', 'max:99'],
        ];
    }
1 like
vincent15000's avatar

@Dmytro_Shved This seems to be fine.

You can also add the rule only if the image is an instance of UploadedFile.

public function getRules()
{
    $rules = [
        'name' => ['required', 'string', 'max:255', \Illuminate\Validation\Rule::unique('recipes')->ignore($this->id)],
    ];

    if ($this->image instanceof \Illuminate\Http\UploadedFile) {
    	$rules['image'] = 'nullable|mimes:jpeg,png,webp|max:2048';
    }

    return $rules;
}

Don't forget that you need to validate the image also when editing the model, but only if the image has been replaced while editing the model.

1 like
dmytroshved's avatar

@vincent15000 I am also getting an error:

League\Flysystem\UnableToRetrieveMetadata 
Unable to retrieve the file_size for file at location: livewire-tmp.

How can I fix it? I didn't found a solution

dmytroshved's avatar
dmytroshved
OP
Best Answer
Level 6

I managed to solve this problem, I've created a new variable public $current_image and like you said:

...I don't care about the image, unless the image is replaced by another one.

So I can show to user $current_image and continue work without extra validation rules

<!-- current photo -->
@if($recipeForm->id)
     <label>Current image</label>
     <div class="mt-2">
          <img src="{{ asset('storage/'. $recipeForm->current_image) }}">
     </div>
@endif
1 like

Please or to participate in this conversation.