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>
required: can't be empty.type="number" min="1": must be a number, at least 1.- The cashier empties Weight and presses Take in laundry: the browser shows a message bubble ("Please fill out this field.", in the browser's language) and doesn't fire the
submitevent. htmx never finds out: 0 requests. - htmx also checks the form itself before sending. If something is invalid, htmx fires
htmx:validation:failedandhtmx:validation:halted, then sends nothing.novalidateon the form turns off both checks. - The browser's check is only a convenience. Anyone can get past it through DevTools, so the server must still check everything.
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>
- The old input comes back through
value, so the cashier doesn't retype it. - The error message sits next to the wrong box.
- In Laravel:
return response()->view('order._form', [...], 422);.
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}
]}'>
- Put it in
<head>. The rules are read from the top, and the first match wins, so422must sit above[45]..(every 4xx and 5xx). "swap": true: a 422 reply is swapped like a successful one. Withhx-target="this"andhx-swap="outerHTML", the old form is replaced by the form with the message."error": true: it still counts as a failure.htmx:responseErrorstill runs, andevent.detail.successfulstaysfalse, soif (event.detail.successful) this.reset()from the form lesson stays correct.- Write all four rules. This list replaces the default list entirely: if it holds only the 422 rule, a 200 reply is no longer swapped.
- On success, the server answers with a fresh empty form, plus
<ul hx-swap-oob="beforeend:#order-board"><li id="order-15">...</li></ul>so the order goes onto the board (the OOB lesson).
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>
blur: when the cashier leaves the Weight box. Since this input sits inside a form and the request isn't a GET, htmx sends all of the form's fields:name=Mrs.%20Tini&weight=25.next .error: the next element that matches.error, the span beside it.- The server answers 200 with its message, or an empty reply when it's fine (which clears the old message). Don't use 422 here, because that reply isn't swapped.
hx-swap="innerHTML"has to be written here: the input inheritshx-swap="outerHTML"from the form. Without it, the span is swapped for plain text, and the next blur can't find.erroranymore.- To check while typing:
hx-trigger="keyup changed delay:500ms", the request goes 500 ms after the last keystroke. Withoutdelay, every letter sends a request.
⚠️ Error messages that never show, or show in the wrong place
- Thinking a 422 shows by itself. Without
responseHandling, a 422 reply isn't swapped and the page sits still. - A
responseHandlingthat holds only the 422 rule. The default rules are gone, and successful replies are no longer swapped. - Forgetting
hx-swap="innerHTML"on the input.hx-swapandhx-targetfrom the form are inherited by the inputs inside it. keyupwith nodelay. One request per letter.- Relying on
requiredalone. The server must still check. - In the exercise: the email input needs
hx-post="/api/validate-email"andhx-trigger="blur", thenhx-target="next .error"so the message goes into the span beside it, not into#form-result, which it inherits from the form.
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.