Form
Thymeleaf source
<sl:form th:object="${form}" action="/rooms" method="post"> <sl:field-group> <sl:field th:field="*{roomName}"> <sl:field-label>Name</sl:field-label> <sl:input/> </sl:field> <sl:field th:field="*{roomCapacity}"> <sl:field-label>Capacity</sl:field-label> <sl:input type="number" min="1"/> </sl:field> <sl:field orientation="horizontal"> <sl:button type="submit">Save</sl:button> </sl:field> </sl:field-group></sl:form>Rendered HTML
<form class="form" action="/rooms" method="post"> <div class="field-group"> <div class="field"> <label class="label field-label" for="roomName">Name</label> <input class="input" type="text" id="roomName" name="roomName" value="Boardroom"> </div> <div class="field"> <label class="label field-label" for="roomCapacity">Capacity</label> <input class="input" type="number" min="1" id="roomCapacity" name="roomCapacity" value="12"> </div> <div class="field" data-orientation="horizontal"> <button class="button" type="submit">Save</button> </div> </div></form><sl:form> renders a <form class="form"> and passes every attribute on: th:object, th:action, method,
novalidate, enctype, hx-*. It has no styling of its own; a <sl:field-group> lays out the
fields. A plain <form> works just as well for a form that is validated when it is submitted: <sl:form> is there
for validate.
<sl:form th:object="${room}" th:action="@{/rooms}" method="post" novalidate> <sl:field-group> <sl:field th:field="*{name}"> <sl:field-label>Name</sl:field-label> <sl:input/> <sl:field-error/> </sl:field> ... </sl:field-group></sl:form>Validating as you type
Section titled “Validating as you type”With validate, every control in a <sl:field> or <sl:field-set> with th:field posts the whole form
to that URL while the user edits it, and swaps its own field from the answer: its error, its aria-invalid and its
aria-describedby. The rules stay where they are, in Bean Validation and your controller; there are none in
JavaScript.
Thymeleaf source
<sl:form th:object="${form}" action="/bookings" th:validate="@{/bookings/validate}" method="post"> <sl:field-group> <sl:field th:field="*{bookingTitle}"> <sl:field-label>Title</sl:field-label> <sl:input/> <sl:field-error/> </sl:field> <sl:field th:field="*{bookingStart}"> <sl:field-label>Start</sl:field-label> <sl:input type="time"/> <sl:field-error/> </sl:field> <sl:field th:field="*{bookingEnd}" depends-on="bookingStart"> <sl:field-label>End</sl:field-label> <sl:input type="time"/> <sl:field-error/> </sl:field> <sl:field orientation="horizontal"> <sl:button type="submit">Book</sl:button> </sl:field> </sl:field-group></sl:form>Rendered HTML
<form class="form" action="/bookings" method="post"> <div class="field-group"> <div class="field" data-validate-field="bookingTitle"> <label class="label field-label" for="bookingTitle">Title</label> <input class="input" type="text" id="bookingTitle" hx-post="/bookings/validate" hx-trigger="input changed delay:200ms" hx-include="closest form" hx-swap="outerMorph" hx-target="closest .field" hx-select="[data-validate-field='bookingTitle']" hx-headers="SL-Validate-Field:'bookingTitle'" name="bookingTitle" value=""> </div> <div class="field" data-validate-field="bookingStart"> <label class="label field-label" for="bookingStart">Start</label> <input class="input" type="time" id="bookingStart" hx-post="/bookings/validate" hx-trigger="change" hx-include="closest form" hx-swap="outerMorph" hx-target="closest .field" hx-select="[data-validate-field='bookingStart']" hx-headers="SL-Validate-Field:'bookingStart'" name="bookingStart" value="10:00"> </div> <div class="field" data-invalid="true" data-validate-field="bookingEnd"> <label class="label field-label" for="bookingEnd">End</label> <input class="input" type="time" aria-invalid="true" id="bookingEnd" aria-describedby="bookingEnd-error" hx-post="/bookings/validate" hx-trigger="change" hx-include="closest form" hx-swap="outerMorph" hx-target="closest .field" hx-select="[data-validate-field='bookingEnd']" hx-headers="SL-Validate-Field:'bookingEnd'" name="bookingEnd" value="09:30"> <div class="field-error" id="bookingEnd-error">The meeting must end after it starts.</div> </div> <div class="field" data-orientation="horizontal"> <button class="button" type="submit">Book</button> </div> </div></form><sl:form th:object="${booking}" th:action="@{/bookings}" th:validate="@{/bookings/validate}" method="post" novalidate> <sl:field-group> <sl:field th:field="*{title}"> <sl:field-label>Title</sl:field-label> <sl:input/> <sl:field-error/> </sl:field> <sl:field th:field="*{start}"> <sl:field-label>Start</sl:field-label> <sl:input type="time"/> <sl:field-error/> </sl:field> <sl:field th:field="*{end}" depends-on="start"> <sl:field-label>End</sl:field-label> <sl:input type="time"/> <sl:field-error/> </sl:field> <sl:button type="submit">Book</sl:button> </sl:field-group></sl:form>The validation endpoint binds and validates the form like the submit does, and renders the same form again, errors
and all. Nothing is saved; the answer is always 200:
@PostMapping("/bookings/validate")String validate(@Valid @ModelAttribute("booking") BookingForm booking, BindingResult bindingResult) { return "bookings/new :: booking-form";}The fragment holds the <sl:form>, with its validate, so each field in the answer is marked as the one on the page
is. It may also be the whole page: htmx takes only the field it asked for.
Sample-01’s “Book a room” page (samples/shadleaf-sample-01, /room-booking) is a complete example: a class-level
constraint for the rules over several fields, a room that may already be booked, and a chain of depends-on.
It needs htmx 4 (htmx.org 4.x, with its hx-alpine-compat extension; see
htmx 2 and htmx 4). It swaps with outerMorph and <hx-partial>, which
htmx 2 does not have: do not use validate with htmx 2. Without htmx, or without JavaScript, the controls are plain
controls and the form is validated when it is submitted, as before. Keep the submit’s own validation either way: the
validation while typing is a help to the user, not a check.
What a control sends
Section titled “What a control sends”Each control gets these attributes. An attribute you write yourself, in any spelling (hx-trigger,
data-hx-trigger, th:hx-trigger, hx:trigger), is kept:
| Attribute | Value |
|---|---|
hx-post |
the validate URL (unless the control has an hx-get, hx-post, hx-put, hx-patch, hx-delete or hx-action of its own) |
hx-trigger |
input changed delay:200ms: 200 ms after the user stops typing. change for a select, checkbox, radio button, date, time, month, week, color and range input. |
hx-include |
closest form |
hx-target |
closest .field, or closest .field-set for a radio group or checkbox group |
hx-select |
[data-validate-field='title']: the field in the answer |
hx-swap |
outerMorph |
hx-headers |
SL-Validate-Field:'title' |
The SL-Validate-Field header holds the request parameter of the field being validated (title,
address.street). The field gets data-validate-field with the same name. A controller can read the header to do
less work, but it does not have to.
The request never uploads files. htmx 4 posts every field of the form, file inputs included, so Shadleaf’s script
takes the files out of a request that has SL-Validate-Field, and a multipart form still posts multipart. A
file upload is not validated as the user types.
Fields that depend on another
Section titled “Fields that depend on another”Some errors come from a rule over several fields: an end time that must come after the start, a confirmation that
must match a password. depends-on on the field that shows the error names the fields it depends on, separated by
spaces:
<sl:field th:field="*{end}" depends-on="start">When the start is validated, the answer also holds the end time’s field, in an <hx-partial>, and htmx swaps both.
That happens only once the end time has a value: a user who has not reached it yet does not get an error there. Put
the error on the dependent field: a class-level constraint that reports on end (with
addPropertyNode("end") in its ConstraintValidatorContext), or bindingResult.rejectValue("end", ...) in the
controller.
What the user notices
Section titled “What the user notices”- Focus and caret stay put. The field is morphed, not replaced: the control that has the focus stays the same element, keeps what the user is typing and where the caret is, and a password keeps its value, though the server never sends it back.
- Fields the user has not touched stay clean. Only the field being edited, and the fields that depend on it once they have a value, are swapped. The rest of the answer is ignored, errors included.
- Errors are not announced. The control becomes
aria-invalidand described by its error, which a screen reader reads when the user reaches it; a live region would talk over every keystroke. The submit still moves the focus to the first error with<sl:form-errors autofocus/>, which does not take the focus while rendering the answer to a validation request. - Select, combobox and date picker keep working. Their scripts add to the server’s markup (the combobox’s input and list, the calendar’s months, the date picker’s button text). Shadleaf’s script keeps those as they are during a validation swap, and gives them only the field’s new state, which they show on the control the user works with.
One control
Section titled “One control”sl:validate does the same for one control, in or outside a <sl:form>. On a Shadleaf control inside a field, it
gets the attributes above:
<sl:field th:field="*{username}"> <sl:field-label>Username</sl:field-label> <sl:input sl:validate="@{/signup/validate}"/> <sl:field-error/></sl:field>A plain element (<input sl:validate="@{/search/validate}">), or a control outside a field, gets only hx-post,
hx-trigger, hx-include and hx-swap: set hx-target and hx-select yourself. In a validating form,
sl:validate="false" on a control leaves it out.
Limits
Section titled “Limits”- In a validating form, the controls that get the attributes are Shadleaf’s, in a field with
th:field:<sl:input>,<sl:textarea>,<sl:native-select>,<sl:select>,<sl:combobox>,<sl:checkbox>(also in a checkbox group),<sl:switch>,<sl:radio-group>,<sl:toggle>and<sl:date-picker>(or<sl:calendar>). A plain<input>in a field is left alone. A<sl:toggle-group>with its ownth:fieldis not validated as the user types. - An
hx-headersof your own on a control replacesSL-Validate-Field: the answer then has no<hx-partial>for the fields that depend on it, and Shadleaf’s script no longer leaves the files out. - The
<hx-partial>finds its field bydata-validate-fieldin the whole page. Two validating forms with a field of the same name on one page would both be swapped.
Attributes
Section titled “Attributes”| Attribute | Values | Default | Description |
|---|---|---|---|
validate | string | — | The URL of the endpoint that validates the form object and renders the form again, such as th:validate="@{/bookings/validate}". Needs htmx 4; without htmx the form posts as usual. |
Every prop can also be set from an expression: th:validate="${...}". Any other attribute passes through to the rendered element, and class is appended.
sl:validate goes on a control (any element), not on <sl:form>:
| Attribute | Value |
|---|---|
sl:validate |
The URL of the validation endpoint: an expression (@{/signup/validate}, ${url}) or a literal (/signup/validate). false leaves the control out of a validating form. |