Documentation menu

Mutation Error

The mutation-error extension emits htmx:onMutationError for POST, PUT, PATCH, and DELETE requests with an HTTP status of 400 or higher.

Attach a handler with h.HxOnMutationError to respond to the event.

Example:

Copy
 1func SaveForm() *h.Element {
 2	return h.Form(
 3		h.PostPartial(Save),
 4		h.NoSwap(),
 5		h.Button(
 6			h.Type("submit"),
 7			h.HxOnMutationError(js.Alert("Save failed.")),
 8			h.Text("Save"),
 9		),
10	)
11}
12
13func Save(ctx *h.RequestContext) *h.Partial {
14	ctx.Fiber.Status(400)
15	return h.EmptyPartial()
16}

The extension also notifies elements with a mutation-error listener, such as a submit button inside the requesting form.