class
UI::FormState
- UI::FormState
- Reference
- Object
Overview
Controlled-input state container threaded through screen renders to preserve text values across rerenders.
Defined in:
ui/form_state.crConstructors
- .new(mount_token : Int64 = 0_i64, initial : Hash(String, String) = {} of String => String)
-
.new(initial : Hash(String, String) = {} of String => String) : self
Used by the iter-2 forward declaration's spec; preserves the old
UI::FormState.newsignature so existing callers don't break.
Class Method Summary
- .current : UI::FormState | Nil
- .current=(fs : UI::FormState | Nil) : UI::FormState | Nil
-
.current_mount_token : Int64
The mount token belonging to
.current. - .current_mount_token=(t : Int64) : Int64
-
.reset_renderer_hooks! : Nil
Reset all renderer-hook state.
Instance Method Summary
- #[](name : String) : String
- #[]?(name : String) : String | Nil
-
#mount_token : Int64
Monotonically-increasing token issued by
UI::ActionDispatcheron every screen mount. -
#register(name : String, initial : String = "") : Nil
Renderer-side hook: when a TextField's
nameis non-empty, the renderer wiresform_state.register(name, view.text)at view- construction so the initial value (e.g. -
#to_h : Hash(String, String)
Snapshot for the controller's
ctx.params. -
#update(name : String, value : String) : Nil
on_change wiring: called by the renderer's callback when the native input fires a change event.
-
#values : Hash(String, String)
Field name -> current value.
Constructor Detail
Used by the iter-2 forward declaration's spec; preserves the
old UI::FormState.new signature so existing callers don't break.
Class Method Detail
The mount token belonging to .current. The dispatcher keeps these
two values in lockstep — every .current= is followed by a token
bump and a .current_mount_token= write. Renderer callbacks
compare their captured token against the live one to detect a
stale fire after navigation.
Reset all renderer-hook state. Used by specs between scenarios and by the dispatcher when tearing down an app.
Instance Method Detail
Monotonically-increasing token issued by UI::ActionDispatcher on
every screen mount. Renderer callbacks capture this at wire-time;
stale callbacks compare their captured token against the
dispatcher's CURRENT token (UI::FormState.current_mount_token)
and short-circuit on mismatch.
Renderer-side hook: when a TextField's name is non-empty, the
renderer wires form_state.register(name, view.text) at view-
construction so the initial value (e.g. pre-populated email after
a failed submit) is in the registry before any on_change fires.
Idempotent: a later #register for the same key DOES update —
the renderer may re-build the same name with a different initial
value when the screen re-renders, and we want the latest one.
Snapshot for the controller's ctx.params. Defensive copy so
callers can't mutate the live registry.
on_change wiring: called by the renderer's callback when the
native input fires a change event. Mount-token check happens at
the call site (in the renderer's visit_text_field) — by the time
#update is called, the stale-callback guard has already passed.