CBWIRE
v4.x
v4.x
  • Introduction
  • How It Works
  • Getting Started
  • Configuration
  • Releases
    • What's New With 4.1
    • What's New With 4.0
    • What's New With 3.2
    • What's New With 3.1
    • What's New With 3.0
    • What's New With 2.2
    • What's New With 2.1
    • What's New With 2.0
  • Resources
  • Upgrading from CBWIRE 3.x
  • Upgrading from CBWIRE 2.x
  • CBWIRE CLI
  • The Essentials
    • Components
    • Templates
    • Data Properties
    • Computed Properties
    • Data Binding
    • Actions
    • Events
    • Nesting Components
    • Lifecycle Methods
    • JavaScript
    • Testing
  • Features
    • Single-file Components
    • Alpine.js
    • Lazy Loading
    • Form Validation
    • File Uploads
    • Query String
    • Redirecting
    • WireBox
  • Template Directives
    • wire:click
    • wire:confirm
    • wire:dirty
    • wire:ignore
    • wire:init
    • wire:key
    • wire:loading
    • wire:model
    • wire:navigate
    • wire:offline
    • wire:poll
    • wire:stream
    • wire:submit
    • wire:transition
  • Advanced
    • Troubleshooting
Powered by GitBook
On this page
  • Overview
  • Live Updating
  • Modifiers
  • Input Fields
  • Text inputs
  • Textarea inputs
  • Single Checkbox
  • Multiple Checkboxes
  • Radio buttons
  • Select dropdowns
  • Dependent select dropdowns
  • Multi-select dropdowns

Was this helpful?

Export as PDF
  1. Template Directives

wire:model

Previouswire:loadingNextwire:navigate

Last updated 7 months ago

Was this helpful?

Overview

You can use wire:model in your to bind to with form inputs.

// ./wires/ContactForm.cfc
component extends="cbwire.models.Component" {
    data = {
        // default values
        "submitted": false,
        "name": "",
        "message": ""
    };
    function sendMessage() {
        data.submitted = true;
        cfmail(
            to="user@somedomain.com",
            from="user@anotherdomain.com",
            subject="Message from " & data.name
        ) {
            writeOutput( data.message )
        }
    }
}
<!--- ./wires/contactform.cfm --->
<cfoutput>
    <form wire:submit="sendMessage">
        <label>
            <span>Name</span>
            <input type="text" wire:model="name"> 
        </label>
        <label>
            <span>Message</span>
            <textarea wire:model="message"></textarea> 
        </label>
        <button type="submit">Send Message</button>
        <cfif submitted>
            Sent
        </cfif>
    </form>
</cfoutput>

When the user submits the form, the server receives a request with the updated data property values from the inputs. An email is sent, and the template is re-rendered with the updated values.

Live Updating

You can use wire:model.live to send property updates to the server as a user types.

<input type="text" wire:model.live="name">

By default, Livewire adds a 150-millisecond debounce to server updates. If the user is continually typing, Livewire will wait until the user stops typing for 150 milliseconds before sending a request.

You can customize the debounce using wire:model.live.debounce.Xms.

<input type="text" wire:model.live.debounce.500ms="name">

Modifiers

Livewire provides various modifiers to control when server requests are sent.

<!--- Update onBlur(), great for validation --->
<input type="text" wire:model.blur="title">

<!--- Update onChange() --->
<select wire:model.change="option">
    ...
</select> 

Below are all the available modifiers:

Modifier
Description

.live

Send updates as user types

.blur

Only send updates on blur event

.change

Only send updates on the change event

.lazy

Alias for .change

.debounce.Xms

Debounce sending updates for X milliseconds

.throttle.Xms

Throttle network request updates by X milliseconds

.number

Cast text input to an integer on the server

.boolean

Cast text input to a boolean on the server

.fill

Use the initial value the "value" HTML attribute provides during page load.

Input Fields

Text inputs

<input type="text" wire:model="name">

Textarea inputs

<textarea type="text" wire:model="content"></textarea>

If the content data property is initialized with a string, Livewire will fill the textarea with its value. Don't do this.

<textarea type="text" wire:model="content">#content#</textarea>

Single Checkbox

<input type="checkbox" wire:model="receiveUpdates">

If the receiveUpdates data property is false, the checkbox will be unchecked. If true, it will be checked.

Multiple Checkboxes

Checkboxes with multiple inputs are stored as an array.

// ./wires/Options.cfc
component extends="cbwire.models.Component" {
    data = {
        "selections": []
    };
}
<!--- ./wires/options.cfm --->
<div>
    <input type="checkbox" value="email" wire:model="selections">
    <input type="checkbox" value="sms" wire:model="selections">
    <input type="checkbox" value="notification" wire:model="selections">
</div>

Radio buttons

<input type="radio" value="yes" wire:model="sendEmail">
<input type="radio" value="no" wire:model="sendEmail">

Select dropdowns

<select wire:model="state">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    ...
</select>

Livewire automatically adds a selected attribute to the option that matches the data property value.

You can dynamically populate these with values.

component extends="cbwire.models.Component" {
    function getStates() {
        return queryExecute( "select id, label from states" );
    }
}
<select wire:model="state">
    <option disabled value="">Select a state</option>
    <cfloop array="#getStates()#" index="state">
        <option value="#state.id#">#state.label#</option>
    </cfloop>
</select>

Dependent select dropdowns

<!--- Select your state --->
<select wire:model.live="state">
    <option disabled value="">Select a state</option>
    <cfloop array="#getStates()#" index="state">
        <option value="#state.id#">#state.label#</option>
    </cfloop>
</select>
<!--- Select city based on state --->
<select wire:model.live="city" wire:key="#state#">
    <option disabled value="">Select a city</option>
    <cfloop array="#getCities( state )#" index="city">
        <option value="#city.id#">#city.label#</option>
    </cfloop>
</select>

Multi-select dropdowns

<select wire:model="states" multiple>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    ...
</select>

When using wire:model, Livewire will only send a network request when an action is performed, like with or . However, you may want to update the server more frequently for things like real-time validation. In those instances, use wire:model.live.

Below shows how to bind various input fields with your .

If you have a dropdown that is dependent on the value of another dropdown and needs to update, you can use to ensure it updates.

templates
data properties
wire:click
wire:submit
data properties
wire:key