#
Form Enhancements with hx-params
, hx-vals
, and hx-validate
By now, you’ve built dynamic forms, submitted data without page reloads, and even added some validation magic using hx-validate
. You’ve seen how htmx makes basic form handling elegant and server-friendly. But real-world forms are rarely basic. Sometimes, you need to customize what gets sent. Sometimes, you want to pass extra values that aren't part of the form. And sometimes, you need to validate in more nuanced ways, without turning to JavaScript.
That’s where this chapter comes in. We’ll explore the finer controls htmx gives you when dealing with form submissions. hx-params
lets you choose exactly which fields get sent. hx-vals
helps you inject values that don’t even exist in the form. And hx-validate
? It goes beyond browser-level constraints, allowing you to create more interactive and user-friendly validation flows while keeping your JavaScript footprint nearly invisible.
This isn’t a rehash of the basics. You’ve already mastered how to submit a form with hx-post
. Here, we’ll fine-tune the experience and make your forms work smarter. You’ll learn how to craft forms that respond to the user’s context, minimize unnecessary data transfer, and provide immediate feedback, all while adhering to the server-driven, Razor Pages-first design.
Once you're comfortable with these advanced form strategies, you'll be well-positioned to tackle the next challenge: optimizing performance. In the following chapter, we’ll explore caching and browser history tricks that make your htmx-powered apps feel lightning-fast.
#
Customizing Form Submission with hx-params
Forms can quickly get messy, especially when they’re long, dynamic, or reused across multiple steps. Sometimes, you don’t want to send every single field to the server on every submission. Maybe you’ve got a multi-step form and only want to process one section at a time. Or perhaps you're using hidden fields to manage layout or state and don’t want them to interfere with your model binding. That’s where hx-params
comes in.
The hx-params attribute gives you control over which fields get included in the request triggered by htmx. By default, htmx submits all fields inside a form when an interaction happens. But you can override this by applying hx-params
to any element that triggers a request, whether it's the