Control UI behavior with wire:dirty in Laravel Livewire v3

Managing form states and providing users with visual feedback for unsaved changes can greatly enhance the user experience in web applications. Laravel Livewire v3 introduces the wire:dirty directive, which allows you to manage dirty state handling in your forms effectively. This feature provides clear visual cues when fields have unsaved changes, ensuring users are aware of modifications that need to be saved. Let’s explore how you can leverage wire:dirty in your Laravel Livewire projects.

Understanding wire:dirty

The wire:dirty directive in Livewire v3 helps you manage and display the dirty state of form inputs. It adds or removes classes and attributes based on whether the input has unsaved changes, allowing you to provide immediate visual feedback to users.

Basic Usage

Here’s a basic example to illustrate how wire:dirty works:

<input wire:model="name" wire:dirty.class="is-dirty">

In this example, the wire:dirty.class="is-dirty" directive adds the is-dirty class to the input field when the name property has unsaved changes.

Real-Life Example

Consider a scenario where you have a user profile form, and you want to highlight fields that have been modified but not yet saved. Here’s how you can implement this using wire:dirty:

use Livewire\Component;

class UserProfile extends Component
{
    public $name;
    public $email;

    public function mount()
    {
        // Load the user's profile data
        $user = auth()->user();
        $this->name = $user->name;
        $this->email = $user->email;
    }

    public function save()
    {
        $user = auth()->user();
        $user->name = $this->name;
        $user->email = $this->email;
        $user->save();

        session()->flash('message', 'Profile updated successfully.');
    }

    public function render()
    {
        return view('livewire.user-profile');
    }
}
<!-- Blade Template (livewire/user-profile.blade.php) -->
<div>
    @if (session()->has('message'))
        <div class="alert alert-success">{{ session('message') }}</div>
    @endif

    <form wire:submit.prevent="save">
        <div class="form-group">
            <label for="name">Name</label>
            <input id="name" type="text" wire:model="name" wire:dirty.class="is-dirty" class="form-control">
        </div>

        <div class="form-group">
            <label for="email">Email</label>
            <input id="email" type="email" wire:model="email" wire:dirty.class="is-dirty" class="form-control">
        </div>

        <button type="submit" class="btn btn-primary">Save</button>
    </form>
</div>

In this example, the wire:dirty.class="is-dirty" directive is used to add the is-dirty class to the name and email input fields whenever they have unsaved changes. This provides a clear visual indication to the user that these fields have been modified.

Benefits of Using wire:dirty

  • Enhanced User Experience: Provide immediate visual feedback for unsaved changes, ensuring users are aware of modifications.
  • Improved Usability: Highlight modified fields, making it easier for users to track changes.
  • Simplified State Management: Automatically manage dirty states without additional JavaScript or complex logic.

Conclusion

The wire:dirty directive in Laravel Livewire v3 is a powerful tool for managing and displaying the dirty state of form inputs. By leveraging this feature, you can enhance user experience, improve usability, and simplify state management in your forms.

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe