brazier/form

Types

Opaque ETS table for CSRF token storage.

pub type CsrfStore

A form field with a value and optional validation errors.

pub type Field {
  Field(name: String, value: String, errors: List(String))
}

Constructors

  • Field(name: String, value: String, errors: List(String))

    Arguments

    name

    The field name (used as the HTML name attribute).

    value

    The current value.

    errors

    Validation errors for this field (empty if valid).

A form with multiple fields and form-level errors.

pub type Form {
  Form(
    fields: List(Field),
    form_errors: List(String),
    csrf_token: String,
  )
}

Constructors

  • Form(
      fields: List(Field),
      form_errors: List(String),
      csrf_token: String,
    )

    Arguments

    fields

    All fields in the form.

    form_errors

    Form-level errors (not tied to a specific field).

    csrf_token

    CSRF token for the form.

Values

pub fn add_error(
  form: Form,
  field_name: String,
  error: String,
) -> Form

Add a validation error to a field.

pub fn add_field(form: Form, name: String, value: String) -> Form

Add a field to the form.

pub fn add_form_error(form: Form, error: String) -> Form

Add a form-level error.

pub fn clear_errors(form: Form) -> Form

Clear all errors from the form.

pub fn create_csrf_store(name: String) -> CsrfStore

Create a session-bound CSRF token store backed by ETS. Tokens are one-time-use: consumed on verification.

pub fn csrf_field(form: Form) -> element.Element(msg)

Render a hidden CSRF input field.

pub fn field_has_errors(form: Form, name: String) -> Bool

Check if a specific field has errors.

pub fn generate_session_csrf(
  store: CsrfStore,
  session_id: String,
  secret: String,
) -> String

Generate a session-bound CSRF token (256 random bits) and store it in ETS, replacing any earlier token for the session. Returns the token. secret is not needed for a random token; the parameter is kept so existing callers compile.

pub fn get_field(form: Form, name: String) -> Result(Field, Nil)

Get a field by name.

pub fn get_value(form: Form, name: String) -> String

Get a field’s value by name, returning empty string if not found.

pub fn has_errors(form: Form) -> Bool

Check if the form has any errors.

pub fn new(secret_key: String) -> Form

Create a new empty form with a CSRF token (256 random bits). secret_key is not needed for a random token; the parameter is kept so existing callers compile.

pub fn password_input(
  form: Form,
  name: String,
  attrs: List(attribute.Attribute(msg)),
) -> element.Element(msg)

Render a password input field. Pass the listener in attrs, for example event.on_input(PasswordChanged).

pub fn select(
  form: Form,
  name: String,
  options: List(#(String, String)),
  attrs: List(attribute.Attribute(msg)),
) -> element.Element(msg)

Render a select dropdown of #(value, label) options. Pass the listener in attrs, for example event.on_change(Picked).

pub fn set_value(form: Form, name: String, value: String) -> Form

Set a field’s value.

pub fn text_input(
  form: Form,
  name: String,
  attrs: List(attribute.Attribute(msg)),
) -> element.Element(msg)

Render a text input field with error display. Pass the listener in attrs, for example event.on_input(NameChanged).

pub fn textarea(
  form: Form,
  name: String,
  attrs: List(attribute.Attribute(msg)),
) -> element.Element(msg)

Render a textarea field. Pass the listener in attrs.

pub fn validate(
  form: Form,
  validators: List(fn(Form) -> Form),
) -> Form

Validate a form by running multiple validation functions.

pub fn validate_email(form: Form, field_name: String) -> Form

Validate that a field matches a regex-like pattern (email format).

pub fn validate_matches(
  form: Form,
  field_name: String,
  other_field: String,
  error_msg: String,
) -> Form

Validate that two fields match (e.g., password confirmation).

pub fn validate_max_length(
  form: Form,
  field_name: String,
  max: Int,
) -> Form

Validate that a field’s value has a maximum length.

pub fn validate_min_length(
  form: Form,
  field_name: String,
  min: Int,
) -> Form

Validate that a field’s value has a minimum length.

pub fn validate_required(form: Form, field_name: String) -> Form

Validate that a field is not empty.

pub fn verify_session_csrf(
  store: CsrfStore,
  session_id: String,
  token: String,
) -> Bool

Verify a session-bound CSRF token against the store. The comparison is constant-time, and the token is consumed atomically on success (one-time: of two concurrent requests with the same token, only one succeeds). A wrong token does not consume the stored one. Returns True if valid, False if invalid or already used.

✨ Search Document