Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 28 ยท Alpine.js Basics

Forms and x-model

Master Alpine.js forms: x-model on text, checkbox, radio, select and textarea, the .number .trim .lazy .debounce modifiers, live validation and @submit.prevent.

Forms are where most interactive pages spend their time, and x-model makes them easy. It creates two-way binding: when the user types, the state property changes, and when your code changes the property, the field updates. You have already seen it on a text input; this lesson covers every common control, the modifiers, live validation and how to handle a submit without leaving the page.

x-model on text, textarea and select

On a text input, textarea or select, x-model binds the field's value to a state property.

Text, textarea and select

The initial value in x-data becomes the initial value of the field (Ann shows in the input and ca selects Canada).

Checkboxes and radio buttons

A single checkbox binds to a boolean. Several checkboxes bound to the same array add and remove their value from it. Radio buttons bound to one property store the value of the selected one.

Checkbox, checkbox group and radio

Notice that the checkbox group needs the state to start as an array. If you started with '' the group would not behave as a list.

Modifiers

Modifiers change when and how the value is copied into the state. You add them with dots.

Modifier Effect
.number Converts the value to a number (or leaves the text if it is not numeric)
.trim Removes whitespace from both ends
.lazy Updates on the change event (leaving the field) instead of every keystroke
.debounce Waits until typing pauses (default 250 ms, or .debounce.500ms)
.throttle Updates at most once per interval
.fill Uses the input's own value attribute as the starting state

Without .number, an <input type="number"> still gives you a string in many cases. Compare the types below.

x-model modifiers compared

Use .lazy or .debounce when each change triggers real work, such as a search or a heavy calculation.

Live validation

Validation falls out of what you already know: write getters that describe whether each field is valid, show messages with x-show, and bind :disabled on the submit button. Show errors only after the user has touched a field, otherwise an empty form greets people with red text.

Live validation with a touched flag

Client-side validation improves the experience, but it is not security. A user can bypass it entirely, so your server must validate again. See Forms, Validation and Submissions for the server side.

Handling submit with @submit.prevent

A form's default submit reloads the page. The .prevent modifier calls event.preventDefault() for you, so Alpine can handle the submission. Put a method on @submit.prevent and read your state in it. In a real page this method would send the data somewhere; in the sandbox we just show it.

Submit without reloading

Pressing Enter inside the input submits the form, which is better accessibility than a click handler on the button alone.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Several checkboxes share x-model="toppings". What should toppings start as?

    With a shared array, checking a box adds its value to the array and unchecking removes it.
  2. 2. What does x-model.lazy change?

    .lazy listens to the change event instead of input.
  3. 3. Why use @submit.prevent on a form?

    .prevent calls event.preventDefault(), so your handler runs and the page stays put.
  4. 4. Is client-side validation with getters enough to protect your data?

    Anyone can send a request without your page. Client validation is for convenience; server validation is for safety.
  5. 5. Which modifier turns the field's text into a number?

    x-model.number casts the value to a number when it is numeric.

Summary

  • x-model binds a field and a property in both directions on text, textarea, select, checkbox and radio controls.
  • A single checkbox is a boolean; checkboxes sharing an array collect values; radios store the chosen value.
  • Modifiers .number, .trim, .lazy, .debounce and .throttle control the type and timing.
  • Validate with getters, show messages with x-show after a field is touched and bind :disabled on the button.
  • Use @submit.prevent to handle submission in Alpine, and always validate again on the server.

Continue with Events, Modifiers and Refs.