Form.* components bind Anvil2 input fields to a formstate field state, removing the boilerplate of wiring value, change, error, and warning props by hand. Each wrapper is a MobX observer, so it re-renders when its bound A2InputFieldState changes.
Installation
Form fields rely onformstate, mobx, and mobx-react as peer dependencies. Install them alongside the package:
Usage
Create anA2InputFieldState, then pass it to the matching Form.* component. The wrapper reads value, reports validation error/warning, and writes changes back to the field state.
name.value to get the current value, call name.validate() to run validation, and check name.hasError / name.error to inspect the result. See Field state for the full API.
Available wrappers
Each wrapper accepts afieldState prop plus the underlying Anvil2 component’s props. The field state’s value type matches the field:
| Component | fieldState value type |
|---|---|
Form.DateFieldRange | { startDate, endDate } | null |
Form.DateFieldSingle | string | null |
Form.DateFieldYearless | YearlessDate | null |
Form.DateFieldYearlessRange | { startDate, endDate } |
Form.NumberField | number | null |
Form.Textarea | string |
Form.TextField | string |
Form.TimeField | string | null |
Selection fields
Form.Select, Form.MultiSelect, and Form.TreeSelect wrap the beta Anvil2 select components and ship from the package root alongside the rest of the namespace. Provide a loadOptions function and bind the selected option(s) to the field state:
Form.Select and Form.MultiSelect support every loader mode (eager, page-lazy, offset-lazy, and group-lazy) — pass the loadOptions function that matches your strategy.
Group fields
Checkbox and radio inputs are provided as the group wrappersForm.RadioGroup and Form.CheckboxGroup. These render an Anvil2 group with a FieldMessage, so validation errors and warnings surface as text. Pass an items array; the field state holds the selected id (Form.RadioGroup) or ids (Form.CheckboxGroup).
Limitations
- Single checkbox and radio inputs have no standalone wrapper. Use
Form.CheckboxGrouporForm.RadioGroup— a bare Anvil2Checkbox/Radioexposes a boolean error (styling only) with no message area, so the group is the form pattern that renders validation messages. - The following Anvil2 input types have no
Form.*wrapper:SearchField,InputMask,Switch,ButtonToggle,SelectCard,Combobox,SegmentedControl, andRichTextEditor.