class FormState
An object that contains form field data, with validation rules.
class FormState<
TSchema extends Record<string, unknown> = Record<string, unknown>,
> extends ObservableObject;
Description
The FormState class provides a data model for user input, allowing UI elements to read and write data from/to a single form state object — instead of having to use individual bindings and event handlers for each field.
The form state object provides methods to get, set, and clear field values, as well as a way to validate current values according to a schema or custom validation functions. Form values and validation errors can be bound to any other view properties to be displayed in the UI.
Validation is performed using a Schema instance. Errors must be added as strings or StringConvertible values (e.g. the result of fmt()) using the required() or error() methods of each field in the schema.
The validation mode determines when fields are validated — see FormState.ValidationMode and FormState.setValidation().
To use a FormState object with UITextField or UIToggle input elements, use their .formStateValue() builder method.
Examples
function FormView(v: Binding<MyActivity>) {
return UI.Column(
UI.TextField().formStateValue(v.bind("form"), "foo"),
UI.Text(v.bind("form.errors.foo.message"))
.hideWhen(v.bind("form.errors.foo").not()),
UI.Button("Go").onClick("Submit")
);
}
class MyActivity extends Activity {
static View = FormView;
form = new FormState((f) => f.object({
foo: f.string().required("Foo is required")
}));
onSubmit() {
let values = this.form.validate();
// now, values.foo is a string with length > 0,
// OR otherwise
// - values is undefined, and
// - this.form.valid is false, and
// - this.form.errors.foo is "Foo is required"
}
}
Constructor
new FormState()
Creates a new instance with the provided validation schema and/or values.
Type members
type FormState.ValidationMode static
A type that determines how form field validation is triggered.
Instance members
validationMode readonly
The current validation mode.values readonly
An object that contains current form field values.errors readonly
An object that contains validation error messages, if any.valid readonly
True if there are currently no recorded errors.set()
Sets the value of the specified form field.clear()
Removes all field values and errors.validate()
Validates the current form values according to the validation schema.validateField()
Validates a single form field.setValidation()
Sets the validation mode for this form state.
Inherited members
emit()
Emits an event, immediately calling all event handlers.emitChange()
Emits a change event.listen()
Adds a handler for all events emitted by this object.listenOnce()
Returns a promise for a single event with the provided name.listenAsync()
Adds a handler for all events emitted by this object, and returns an async iterable.observe()
Observes a property, a bound property from an attached parent, or an observable object.observeAsync()
Observes one or more targets asynchronously, batching or debouncing/throttling updates.attach() protected
Attaches the specified observable object to this object.isUnlinked()
Returns true if the object has been unlinked.unlink()
Unlinks this observable object.beforeUnlink() protected
A method that’s called immediately before unlinking an object, can be overridden.
