I would suggest watching the Larvel in 30 days and the Livewire from scratch courses.
They both will cover validation.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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:
class RecipeWizard extends Component
{
use WithFileUploads;
public Forms\RecipeForm $recipeForm;
public Forms\IngredientsForm $ingredientsForm;
public Forms\GuideForm $guideForm;
public $form_step = 1;
public $dishCategories;
public $cuisines;
public $menus;
public $units;
public function mount(Recipe $recipe): void
{
$this->dishCategories = DishCategory::get();
$this->cuisines = Cuisine::get();
$this->menus = Menu::get();
$this->units = Unit::get();
// set the data if $id exists
if (isset($recipe->id)){
$this->recipeForm->setRecipe($recipe); // this code below
$this->ingredientsForm->setIngredients($recipe->ingredients);
$this->guideForm->setGuide($recipe->guideSteps);
}
}
public function render(): View
{
return view('livewire.recipe-wizard');
}
public function next_step(): void
{
$this->resetErrorBag();
$this->validateFields();
$this->form_step++;
}
// ...skipped not important code
public function validateFields(): void
{
if ($this->form_step == 1){
$this->recipeForm->validate();
}else if ($this->form_step == 2){
$this->ingredientsForm->validate();
}
}
public function store(): void
{
$this->resetErrorBag();
if ($this->form_step == 3){
$this->guideForm->validate();
}
$finalIngredients = $this->ingredientsForm->prepareFinalIngredients();
$recipe = $this->recipeForm->createRecipe($finalIngredients);
$this->guideForm->insertGroupedSteps($recipe->id);
session()->flash('recipe_created', 'Recipe created successfully!');
$this->redirect('/recipes/create');
}
}
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
Please or to participate in this conversation.