Money field

Money field forms CompositeController

Major-unit decimal input over a hidden minor-unit carrier — the form posts integer minor units, never floats.

Layer: L1 surface · Recipe: money-minor-units — money as minor-unit integer. Curriculum: AGENTS.md; pick matrix: docs/agent/pick-a-surface.md; blast radius: CONSUMER_MAP.md.

Copy this

html
<div class="money hm-measure" data-money data-currency="GBP" data-scale="2">
  <div class="form-money-group"><span class="form-money-prefix" aria-hidden="true">£</span><input type="text" inputmode="decimal" id="hm-money-input" value="15.00" class="form-input form-input-trailing" placeholder="0.00" aria-label="Amount (GBP)"></div>
  <input type="hidden" name="amount_minor" value="1500">
  <input type="hidden" name="amount_currency" value="GBP">
</div>

Server exchange

This Hyperpart has no server exchange — it is presentation or client chrome only. htmx does not issue a request on this part's behalf. If you put an affordance (hx-*) on a control that uses this markup, that action's exchange belongs to the action, not this part. See Swap contract for host-owned envelopes.

Swap contract

Agent-visible HTMX topology (ADR-0054 / decision 0012). exchange envelope = what the response may re-emit relative to the persistent slot (body_only | outer | none | host_owned | document). dual-lock validates part markup only — not this envelope. Stem: stems/morph-safe-hypermedia.md; decision: docs/decisions/0012-swap-identity-contract.md.

No host HTMX exchange on this part — presentation or client chrome only. exchange envelope: n/a.

If a host wraps this markup in hx-*, that host owns the swap contract (sole identity + envelope). Prefer innerMorph / outerMorph for stable slots; replacement for flash; body-only responses under inner swaps.

Envelope response examples

This part has no owned exchange (envelope n/a). If a host adds hx-*, that host’s envelope applies — typically body_only:

html
<!-- Host wraps this presentation part with hx-* (host owns envelope) -->
<!-- Prefer: hx-swap="innerMorph" hx-target="#panel-body" -->
<!-- Server returns body_only interior for #panel-body -->
<div class="dz-stack">content…</div>

Do not re-own the slot:

html
<!-- WRONG: server returns the presentation root with a new id every poll -->
<div id="money-root" data-dz-region>…</div>

How to use it

Seams

  • root data-scale is the conversion factor; hidden *_minor carrier is the submit value
  • selector mode reads currency <select> data-scale / data-symbol options

Do / Don't

DoDon't
keep the minor integer in a hidden input the form postspost the formatted display string as the money value

Pitfalls

  • display value is SERVER-computed from the minor carrier — no client init pass
  • empty blur clears the carrier; never invent a client-side float source of truth
  • invalid text (`abc`, `12abc`) clears the carrier — do not post 0 or rewrite to `0.00`

Keyboard / AT

  • display input is a normal text field; AT reads the typed amount
  • currency select changes retune scale and prefix without remounting

Related parts

field

DOM contract

What the emitted HTML must satisfy — the table is the required surface; Python under contracts/ is the package-internal dual-lock CI runs (tests/test_contracts.py), not an app route. Standalone HTMX4: implement the API so responses match this markup. Dazzle: the agent emits SSR that already satisfies it. Do not invent attrs outside these tables. For request/response wiring see Server exchange.

contracts/money.py

Required in the DOM: root [data-money] (part money). Emit only these attributes — inventing extras is fine only if controllers ignore them; omitting required ones fails CI (tests/test_contracts.py).

NodeAttrConstraint
[data-money]data-scalepresent (any value)
[data-money]data-currencypresent (any value)

Ingestion model MoneyField

Server-side shape before render — one normalisation boundary for producers.

FieldTypeRequired
namestringrequired
currencystringoptional
scaleintegeroptional
major_displaystringoptional
minor_valueintegeroptional
field_idstringoptional

Exemplar render()

Executable in CI: the Python below is render(); the boxed preview is render(EXEMPLARS[0]) — the first fixture the dual-lock tests emit, not a separate widget and not gallery mock data.

python
def render(field: MoneyField) -> str:
    fid = html.escape(field.field_id, quote=True)
    name = html.escape(field.name, quote=True)
    cur = html.escape(field.currency, quote=True)
    # aria-label on the major input — dual-lock fragment has no visible
    # label sibling; axe label rule requires an accessible name.
    return (
        f'<div class="dz-money" data-dz-money '
        f'data-dz-currency="{cur}" '
        f'data-dz-scale="{field.scale}">'
        f'<input id="{fid}" name="{name}" inputmode="decimal" '
        f'value="{html.escape(field.major_display, quote=True)}" '
        f'class="dz-form-input" aria-label="Amount ({cur})">'
        f'<input type="hidden" name="{name}_minor" value="{field.minor_value}">'
        f"</div>"
    )

Live output of render(EXEMPLARS[0]) — fixture markup the dual-lock validates (sample field values only).

Notes

State-in-DOM: the root's data-scale is the conversion factor; money.js keeps the hidden *_minor carrier in sync on input, normalizes the display to toFixed(scale) on blur (empty clears the carrier), and — in selector mode — reads a currency <select>'s data-scale/data-symbol options to retune scale and prefix. The edit-mode display value is SERVER-computed from the minor carrier, so there is no client init pass.

Source files

One logical Hyperpart, 4 code items (CSS layered, JS bundled). Bound by HYPERPART: money — python tools/hyperpart.py money lists them.

site/registry.py · contracts/money.py · components/form.css:156 · controllers/dz-money.js

Composed of

Field