htmx Form Validation

The browser checks first (required, min) and holds back the submit with no request. The server checks again, including what only it knows, and answers 422…

The cashier types Mrs. Tini, 25 kg, and presses Take in laundry. The server refuses, because today's machines only have room for 20 kg more. What the cashier needs now: a message next to the weight box, and a name she doesn't have to type again. What often happens is the opposite: the button does nothing, or every box is wiped.

Form validation means checking the input before it's accepted. There are two layers: the browser checks first through HTML attributes such as required, then the server checks again, including things only the server knows. The counter at Mrs. Rini's Laundry works the same way. The cashier turns away a slip with an empty weight box, right at the counter. A slip that passes goes to the back, and the back checks whether today's machines still have room. If not, the slip comes back with a red note on the wrong box, and the other boxes stay filled.

Layer 1: the browser checks first

<form id="intake" hx-post="/order" hx-target="this" hx-swap="outerHTML">
  <input name="name" placeholder="Name" required>
  <input name="weight" type="number" min="1" placeholder="kg" required>
  <span class="error"></span>
  <button>Take in laundry</button>
</form>

Layer 2: the server refuses with a 422

Mrs. Rini's machines hold 30 kg a day. The board already has 10 kg (3 + 2 + 5), so 20 kg is left. The browser doesn't know that number, only the server does. A 25 kg order passes min="1", then the server answers with status 422 Unprocessable Content ("the input arrived, but it's refused") and the same form, holding a message:

<form id="intake" hx-post="/order" hx-target="this" hx-swap="outerHTML">
  <input name="name" placeholder="Name" required value="Mrs. Tini">
  <input name="weight" type="number" min="1" placeholder="kg" required value="25" aria-invalid="true">
  <span class="error">Only 20 kg of machine room is left today.</span>
  <button>Take in laundry</button>
</form>

A 422 isn't swapped

In htmx 2, 4xx and 5xx replies aren't swapped. htmx fires htmx:responseError, and the form stays as it was, with no message at all. The cashier only sees a button that doesn't react.

responseHandling: let a 422 be swapped

<meta name="htmx-config" content='{"responseHandling": [
  {"code": "204", "swap": false},
  {"code": "[23]..", "swap": true},
  {"code": "422", "swap": true, "error": true},
  {"code": "[45]..", "swap": false, "error": true}
]}'>

Check one box before the submit

<input name="weight" type="number" min="1" required
       hx-post="/order/check-weight" hx-trigger="blur"
       hx-target="next .error" hx-swap="innerHTML">
<span class="error"></span>

⚠️ Error messages that never show, or show in the wrong place

TL;DR: The browser checks first (required, min) and holds back the submit with no request. The server checks again, including what only it knows, and answers 422 with the form holding the message and the old input. htmx 2 doesn't swap a 422 unless responseHandling in <meta name="htmx-config"> allows it: write all four rules, with 422 set to "swap": true, "error": true above [45]... To check one box: hx-post, hx-trigger="blur" (or keyup changed delay:500ms), hx-target="next .error" and hx-swap="innerHTML", with the server answering 200 with a message or nothing.

⚡ Layer 1: the browser checks first

Write:

<form id="intake" hx-post="/order" hx-target="this" hx-swap="outerHTML">
  <input name="name" placeholder="Name" required>
  <input name="weight" type="number" min="1" placeholder="kg" required>
  <span class="error"></span>
  <button>Take in laundry</button>
</form>

Meaning: With the Weight box emptied, the browser shows a message bubble and doesn't fire submit, so there are 0 requests. The browser's check is only a convenience: the server must still check everything.

⚡ Layer 2: the server refuses with a 422

Guess first: the server answers 422 with the form holding an error message. With no settings at all, what shows?

Nothing. htmx 2 doesn't swap 4xx and 5xx replies, so the form sits still with no message.

Meaning: Write responseHandling in <meta name="htmx-config">, all four rules, with 422 above [45].. and set to "swap": true, "error": true.

⚡ Check one box when it's left

Write:

<input name="weight" type="number" min="1" required
       hx-post="/order/check-weight" hx-trigger="blur"
       hx-target="next .error" hx-swap="innerHTML">
<span class="error"></span>

Meaning: blur runs when the Weight box is left, and the message goes into the .error beside it. To check while typing, use keyup changed delay:500ms. Without delay, every letter sends a request.

What you will learn