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

dmytroshved's avatar

Question about validation in form for create/update in Livewire 3

I have a RecipeWizard.php form that should both create and update recipes.

I've added form objects to improve readability. Right now I'm thinking about how to validate the data when it comes to updating a recipe.

My main difficulties arise when I need to validate a recipe name and the photo that the user provided for the recipe. The name has a required rule upon creation, and the recipe photo, after creation, is a string (because it's the path to the photo from the project's storage directory).

And here some pieces of code (I dont want to paste all code because it will be too much)

RecipeWizard.php:


rules() from RecipeForm $recipeForm:


// real time image validation
#[Validate]
    #[Rule(['nullable','mimes:jpeg,png,webp'])]
    public $image;
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'],
        ];
    }

->setRecipe(Recipe $recipe)


public function setRecipe(Recipe $recipe): void
    {
        $this->id = $recipe->id;
        $this->name = $recipe->name;
        $this->description = $recipe->description;
        $this->image = $recipe->image;
        $this->cook_time = $recipe->cook_time;
        $this->servings = $recipe->servings;

        $this->category = $recipe->dish_category_id;
        $this->cuisine = $recipe->cuisine_id;
        $this->menu = $recipe->menu_id;
    }

be grateful for some help

0 likes
1 reply
Tray2's avatar

I would suggest watching the Larvel in 30 days and the Livewire from scratch courses.

They both will cover validation.

Please or to participate in this conversation.