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

newbie360's avatar

Refactor Livewire to use action class

Sorry for this example post abit long...

My question is should i bundle validation in the action class?

blade

    {{-- Modal for create or edit --}}
    <form wire:submit="formAction">
        // ...
    </form>

component

form object ref: https://github.com/livewire/livewire/blob/f18f9d8205492ba9a292717fea5adde079e8aa95/src/Features/SupportFormObjects/Form.php#L34

as you can see, there is nothing about rules and validation in the livewire

here is the action class bundle validation

should i bundle validation in the action class? or any bad effect ?

0 likes
1 reply
ramonrietdijk's avatar

To start off, I don't think either way is bad. As the CreatePost class is responsible for creating a new model, I'd personally handle the validation upfront. It would be the easiest to add attributes or a rules method to do so. Otherwise, you would be performing the validation twice.

Taking care of the validation in the Livewire component would respect the separation of concerns and the single-responsibility principle in SOLID.

Please or to participate in this conversation.