DEV Community

Discussion on: Question: How to better structure Laravel Livewire code and components?

Collapse
 
messerli90 profile image
Michael Messerli

A little late to respond but one accepted way to extract validation logic is to add it to the model. It looks like this:

// Model.php
public static function validationRules()
    {
        return [
            'name' => ['required', 'string', 'max:250'],
            'other_attr' => ['nullable', 'url', 'max:250'],
            'another_attr' => ['nullable', 'string', 'max:250']
        ];
    }

Then in Livewire you can do:

// Livewire component
public function store() {
    $this->validate(Model::validationRules);
}
Collapse
 
abrardev99 profile image
Abrar Ahmad

That's pretty nice. But it will be more pretty strecture like CustomFormRequest. Anyway Thank You so much