class UI::FormState

Overview

Controlled-input state container threaded through screen renders to preserve text values across rerenders.

Defined in:

ui/form_state.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(mount_token : Int64 = 0_i64, initial : Hash(String, String) = {} of String => String) #

[View source]
def self.new(initial : Hash(String, String) = {} of String => String) : self #

Used by the iter-2 forward declaration's spec; preserves the old UI::FormState.new signature so existing callers don't break.


[View source]

Class Method Detail

def self.current : UI::FormState | Nil #

[View source]
def self.current=(fs : UI::FormState | Nil) : UI::FormState | Nil #

[View source]
def self.current_mount_token : Int64 #

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.


[View source]
def self.current_mount_token=(t : Int64) : Int64 #

[View source]
def self.reset_renderer_hooks! : Nil #

Reset all renderer-hook state. Used by specs between scenarios and by the dispatcher when tearing down an app.


[View source]

Instance Method Detail

def [](name : String) : String #

[View source]
def []?(name : String) : String | Nil #

[View source]
def mount_token : Int64 #

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.


[View source]
def register(name : String, initial : String = "") : Nil #

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.


[View source]
def to_h : Hash(String, String) #

Snapshot for the controller's ctx.params. Defensive copy so callers can't mutate the live registry.


[View source]
def update(name : String, value : String) : Nil #

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.


[View source]
def values : Hash(String, String) #

Field name -> current value. Populated by #register (initial seed from view.text) and #update (on_change from input).


[View source]