Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 39 ยท Components and Props

Forms and Controlled Inputs in SolidJS

Build forms in SolidJS: bind inputs, checkboxes, radios and selects to signals, validate with memos, handle submit, and wrap fields in reusable components.

Forms combine everything from this chapter: signals for state, props for reusable fields, events for input and submit, and derived values for validation. Solid has no special form library built in, and it does not need one. A controlled input is just a signal plus two attributes.

The controlled input pattern

JSX
const [name, setName] = createSignal("");

<input value={name()} onInput={(e) => setName(e.currentTarget.value)} />
  • value={name()} writes the signal into the DOM property whenever it changes.
  • onInput writes the DOM value back into the signal on every keystroke.

Use onInput for live updates; onChange fires only when the field commits (on blur or Enter). Because only the one value attribute is reactive, typing updates a single text node elsewhere, not the whole form.

A live-bound text input

Press clear and the input empties, because the DOM value follows the signal in both directions.

Checkboxes, radios, selects and textareas

Each control uses its natural property and event.

JSX
<input type="checkbox" checked={agree()} onChange={(e) => setAgree(e.currentTarget.checked)} />

<input type="radio" name="plan" value="pro"
       checked={plan() === "pro"} onChange={() => setPlan("pro")} />

<select value={color()} onChange={(e) => setColor(e.currentTarget.value)}>
  <option value="red">Red</option>
  <option value="blue">Blue</option>
</select>

<textarea value={bio()} onInput={(e) => setBio(e.currentTarget.value)} />

Notes:

  • Checkboxes use checked and the change event (or click).
  • For radios, compare the signal with the radio's value and set it on change.
  • For <select>, set value on the select; Solid applies it after the options exist.
  • A <textarea> takes its content from value, not from children.
  • For number fields use e.currentTarget.valueAsNumber or Number(value), and remember an empty field gives NaN.

Validation with derived state

Validation is a derived value: a function or memo over the field signals. There is nothing to "trigger".

JSX
const emailError = () => (/^\S+@\S+\.\S+$/.test(email()) ? "" : "Enter a valid email");
const canSubmit = () => !emailError() && agree();

Show errors only after a field has been touched so you do not scold users while they type their first character. Keep a touched signal set on onBlur, and show the message with <Show when={touched() && emailError()}>.

A signup form with validation and submit

The submit handler prints the data on the page, which is how forms are demonstrated safely here; a real app would send it with fetch.

Reusable field components

Wrap the repeated markup (label, input, error) in a component that takes the value and an updater. Use splitProps to forward native attributes.

JSX
function TextField(props) {
  const [local, rest] = splitProps(props, ["label", "value", "onValue", "error"]);
  return (
    <label>
      {local.label}
      <input value={local.value} onInput={(e) => local.onValue(e.currentTarget.value)} {...rest} />
      <Show when={local.error}><span class="err">{local.error}</span></Show>
    </label>
  );
}

<TextField label="City" value={city()} onValue={setCity} error={cityError()} />

Passing the setter as a callback prop (onValue={setCity}) is the Solid equivalent of two-way binding: the child never owns the state, and the parent stays the single source of truth.

Many fields: one signal or a store

For a handful of fields, one signal each is simplest. For a large form, group the values in a store (see Stores with createStore) and update one key at a time, for example setForm("email", value). Dedicated libraries exist for complex forms (@modular-forms/solid, @felte/solid), but the primitives above carry you a long way.

Uncontrolled forms and FormData

You do not have to bind every input. On submit you can read the whole form natively:

JSX
const onSubmit = (e) => {
  e.preventDefault();
  const data = Object.fromEntries(new FormData(e.currentTarget));
  save(data);
};

Controlled signals are best when you need live validation or dependent fields; FormData is enough for simple submissions.

Common mistakes

  • Using onChange for live validation. It fires only on commit; use onInput.
  • Forgetting e.preventDefault() in onSubmit, so the page reloads.
  • Setting value without onInput. The field resets when the signal re-evaluates, and typing seems ignored.
  • Reading e.target.value after an await. Read it synchronously, or store it first.
  • Binding objects to inputs by mutating them. Create new values, or use a store setter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two attributes make a controlled text input?

    value pushes the signal into the DOM and onInput pushes the DOM value back.
  2. 2. Which event keeps a text signal updated on every keystroke?

    input fires on every edit; change fires on commit.
  3. 3. How is form validation best expressed?

    Validation is derived state, so a function over the signals is always up to date.
  4. 4. What does passing the setter as a prop (onValue={setCity}) achieve?

    The child reports changes upward; the parent owns the state and passes it down.

Summary

  • A controlled input is value={signal()} plus onInput writing back.
  • Checkboxes use checked, radios compare values, selects and textareas use value.
  • Validation is derived state; show errors after the field is touched.
  • Wrap fields in components with splitProps; pass setters as callback props.
  • Use FormData for simple, uncontrolled forms, and always prevent the default submit.

Next: Stores with createStore