Module forms

Module forms 

Source
Expand description

Form handling, building, and validation for HTMX applications

This module provides a builder-pattern API for creating forms with:

  • Automatic CSRF token injection
  • HTMX attribute support
  • Integration with the validator crate
  • Field-level error rendering

§Quick Start

use acton_htmx::forms::{FormBuilder, InputType};

let form = FormBuilder::new("/users", "POST")
    .csrf_token("abc123")
    .field("email", InputType::Email)
        .label("Email Address")
        .required()
        .placeholder("you@example.com")
        .done()
    .field("password", InputType::Password)
        .label("Password")
        .required()
        .min_length(8)
        .done()
    .submit("Sign Up")
    .htmx_post("/users")
    .htmx_target("#result")
    .htmx_swap("innerHTML")
    .build();

println!("{form}");

§HTMX Integration

Forms can be enhanced with HTMX attributes for seamless partial updates:

use acton_htmx::forms::FormBuilder;

let form = FormBuilder::new("/search", "GET")
    .htmx_get("/search")
    .htmx_trigger("keyup changed delay:500ms")
    .htmx_target("#results")
    .htmx_swap("innerHTML")
    .htmx_indicator("#spinner")
    .build();

§Validation Errors

Display validation errors alongside fields:

use acton_htmx::forms::{FormBuilder, InputType, ValidationErrors};

let mut errors = ValidationErrors::new();
errors.add("email", "Invalid email address");

let form = FormBuilder::new("/users", "POST")
    .errors(&errors)
    .field("email", InputType::Email)
        .label("Email")
        .done()
    .build();

// Errors are automatically rendered next to the field

Structs§

FieldBuilder
Builder for input fields
FieldError
A single validation error for a field
FileFieldAttrs
File upload-specific attributes for file input fields
FileFieldBuilder
Builder for file upload fields
FormBuilder
Builder for constructing HTML forms
FormField
A form field with all its attributes
FormRenderOptions
Options for customizing form rendering
FormRenderer
Renders forms to HTML
SelectOption
Option for select dropdowns
TemplateFormRenderer
Renders forms using minijinja templates
ValidationErrors
Collection of validation errors keyed by field name

Enums§

FormRenderError
Errors that can occur during form rendering
InputType
HTML input types