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

LucasR's avatar

Livewire Forms and validation of create/update

I'm a happy Livewire user, that recently did the 2.x to 3.x transition

Overall it has been a somewhat smooth ride, however, I'm guessing I fail at handling validation in Livewire Forms correctly, so hoping someone can give a bit of pointers :)

Quite often I find myself where I have a set of validation rules, where the rules required differs greatly between create operations and update operations.

e.g. I might have a create form where I can fill out let's say 10 fields, but the update form one can only modify 5 fields. A lot of my site has some kind of relationship to other models, whether it's being one to one, or one to many. When I create an item, I might have to select 3 other models that my item links to.

However, when I go and update that item, the only fields I expose are things for that model itself. The relations are always the same after creation.

I'm not always super satisfied with required_if, or required_unless, exclude_if, exclude_unless, since the logic that happens isn't always very clear directly when working with a lot of fields that all have some kind of required_if or required_unless , etc.

My question I guess boils down to, how do you handle such cases where one or more fields might not be needed when updating a model?

I could easily create a create Form and an update Form, however, then I feel I defeats the purpose of even using forms in the first place, if I have to redefine the same validation rules across two forms (and keep the two forms' rules in sync).

0 likes
1 reply
LaryAI's avatar
Level 58

Handling different validation rules for create and update operations in Livewire can indeed be a bit tricky, but there are several strategies you can use to manage this effectively. Here’s a solution that leverages Livewire’s lifecycle hooks and conditional validation rules to handle this scenario.

Step-by-Step Solution

  1. Define Separate Validation Rules: Define separate validation rules for create and update operations within your Livewire component.

  2. Use Conditional Logic: Use conditional logic to apply the appropriate validation rules based on the operation being performed.

  3. Leverage Lifecycle Hooks: Utilize Livewire’s lifecycle hooks to set the mode (create or update) and apply the corresponding validation rules.

Example Code

Here’s an example of how you can implement this in a Livewire component:

use Livewire\Component;

class ItemForm extends Component
{
    public $item;
    public $isUpdate = false;

    // Define your fields here
    public $field1;
    public $field2;
    public $field3;
    // ... other fields

    // Define your validation rules
    protected function rules()
    {
        if ($this->isUpdate) {
            return [
                'field1' => 'required|string|max:255',
                'field2' => 'nullable|string|max:255',
                // Only validate the fields that are needed for update
            ];
        } else {
            return [
                'field1' => 'required|string|max:255',
                'field2' => 'required|string|max:255',
                'field3' => 'required|integer',
                // Validate all fields for create
            ];
        }
    }

    // Method to handle create
    public function create()
    {
        $this->isUpdate = false;
        $this->validate();

        // Create logic here
        Item::create([
            'field1' => $this->field1,
            'field2' => $this->field2,
            'field3' => $this->field3,
            // ... other fields
        ]);

        // Reset fields after creation
        $this->resetFields();
    }

    // Method to handle update
    public function update()
    {
        $this->isUpdate = true;
        $this->validate();

        // Update logic here
        $this->item->update([
            'field1' => $this->field1,
            'field2' => $this->field2,
            // Only update the fields that are needed for update
        ]);

        // Reset fields after update
        $this->resetFields();
    }

    // Method to reset fields
    private function resetFields()
    {
        $this->field1 = '';
        $this->field2 = '';
        $this->field3 = '';
        // ... reset other fields
    }

    public function render()
    {
        return view('livewire.item-form');
    }
}

Explanation

  1. Conditional Validation Rules: The rules method checks the $isUpdate property to determine which set of validation rules to apply. This allows you to have different validation rules for create and update operations.

  2. Lifecycle Hooks: The create and update methods set the $isUpdate property accordingly before calling validate(). This ensures that the correct validation rules are applied based on the operation.

  3. Reset Fields: After performing the create or update operation, the resetFields method is called to clear the form fields.

Benefits

  • Single Form Component: You maintain a single form component, reducing code duplication and keeping your validation logic centralized.

  • Clear Logic: The conditional logic within the rules method makes it clear which rules apply to create and update operations, improving maintainability.

This approach should help you manage different validation rules for create and update operations in Livewire more effectively.

Please or to participate in this conversation.