wire:model

The wire:model directive creates two-way data binding between form inputs and component data properties. As users type or interact with form elements, the values are automatically synchronized with your server-side data, enabling reactive forms without manual event handling.

Basic Usage

This user profile form demonstrates wire:model with various input types and live updating:

// wires/UserProfile.bx
class extends="cbwire.models.Component" {
    data = {
        "name": "",
        "email": "",
        "bio": "",
        "notifications": true,
        "preferences": [],
        "country": ""
    };

    function save() {
        // Save user profile
        sleep(1000);
    }
}

What wire:model Does

When you add wire:model to an input, CBWIRE automatically:

  • Binds Data: Creates two-way binding between input values and component properties

  • Syncs Changes: Updates server data when users interact with form elements

  • Preserves State: Maintains input values across component re-renders

  • Handles Types: Automatically manages different input types (text, checkbox, select, etc.)

Available Modifiers

The wire:model directive supports these modifiers:

  • Live: .live - Sends updates as user types (with 150ms debounce)

  • Blur: .blur - Only sends updates when input loses focus

  • Change: .change - Only sends updates on change event

  • Lazy: .lazy - Prevents server updates until an action is called

  • Debounce: .debounce.Xms - Customizes debounce timing (e.g., .debounce.500ms)

  • Throttle: .throttle.Xms - Throttles network requests by X milliseconds

  • Number: .number - Casts text input to integer on server

  • Boolean: .boolean - Casts text input to boolean on server

  • Fill: .fill - Uses initial HTML value attribute during page load

Combine modifiers as needed: wire:model.live.debounce.500ms="username"

Input Types

Text Inputs

Textarea

Single Checkbox

The checkbox will be checked if the data property is true, unchecked if false.

Multiple Checkboxes

Radio Buttons

Select Dropdowns

Livewire automatically adds the selected attribute to the matching option.

Dynamic Select Options

Dependent Dropdowns

Use wire:key to ensure dependent dropdowns update properly:

Multi-Select Dropdowns

By default, wire:model only sends updates when actions are performed. Use wire:model.live for real-time validation or immediate server updates as users type.

Was this helpful?