Skip to content

Form Initialization ​

The Form Constructor accepts 2 arguments:


First Constructor Argument ​

The first argument defines the fields and their properties. It supports two modes:

  • Unified — all field props in one fields object
  • Separated — props split across parallel objects (values, labels, rules, etc.)

See Defining Fields for a detailed comparison.

Structure ​

PropertyDescriptionHelp
structField structure as an array of string paths (dot + [] notation). Only needed for separated mode.—
fieldsUnified mode: an object of field definitions. Separated mode: an array of field path strings.Defining Fields

Field Properties (Separated Mode) ​

Props are passed as parallel objects keyed by field name or dot/array path.

Values & Display ​

Prop KeyDescriptionHelp
valuesInitial values for each fieldFields docs
labelsHuman-readable field labelsFields docs
placeholdersPlaceholder text for inputsFields docs
initialsInitial values (fallback if value is not set)Fields docs
defaultsDefault values (applied on reset())Fields docs

Behavior & State ​

Prop KeyDescriptionHelp
disabledDisabled state per fieldFields docs
deletedSoft-deleted state (needs softDelete option)Fields docs
autoFocusSet to true on the first field to focus on init—
inputModeMobile keyboard mode: none, text, decimal, numeric, tel, search, email, url—
nullableAllow null field values—
autoCompleteHTML autocomplete attribute for the field—
extraExtra metadata (useful for select option lists)Fields docs
optionsPer-field options (same keys as Form Options, fallback to form-level)Form Options
bindingsName of the binding rewriter/template to use for this fieldBindings

Validation ​

Prop KeyDescriptionHelp
rulesDVR validation rules (e.g. 'required|email')DVR setup
validatorsVJF validation functionsVJF setup
validatedWithValidate a different field prop instead of value—
relatedOther field paths to validate together with this fieldFields docs

Converters & Computed ​

Prop KeyDescriptionHelp
computedFunction ({ form, field }) => value for dynamic field valuesComputed Props
convertersArray of converter functions applied to the field value—
inputInput converter function (user input → stored value)Converters
outputOutput converter function (stored value → output value)Converters

MobX Events ​

Prop KeyDescriptionHelp
observersMobX observers on field props or fields map changesObserve / Intercept
interceptorsMobX interceptors on field props or fields map changesObserve / Intercept

Event Hooks & Handlers ​

Prop KeyDescriptionHelp
hooksEvent hook functions. Available: onInit, onChange, onToggle, onFocus, onBlur, onDrop, onSubmit, onSuccess, onError, onClear, onReset, onAdd, onDel, onKeyUp, onKeyDownEvent Hooks
handlersEvent handler functions. Available: onChange, onToggle, onFocus, onBlur, onDrop, onSubmit, onClear, onReset, onAdd, onDel, onKeyUp, onKeyDownEvent Handlers

Custom Field Classes ​

Prop KeyDescriptionHelp
classesCustom Field classes (must extend Form.Field)Extend in fields definition

Note: In unified mode, all props above are inlined inside each field definition object instead of being split across separate objects. See Defining Fields for examples of both modes.


Second Constructor Argument ​

The second argument configures plugins, options, bindings, and form-level hooks/handlers.

PropertyDescriptionHelp
pluginsEnable validation via external librariesValidation Plugins
optionsForm-level options (validation behavior, error display, etc.)Form Options
bindingsDefine how field props are passed to input componentsProps Bindings
extraInject extra objects accessible as form.extra—
hooksForm-level event hooks. Available: onInit, onSubmit, onSuccess, onError, onClear, onReset, onAdd, onDelEvent Hooks
handlersForm-level event handlers. Available: onSubmit, onClear, onReset, onAdd, onDelEvent Handlers

Constructor Usage ​

javascript
import { Form } from 'mobx-formkit';

// Unified fields only
new Form({ fields });

// Unified + plugins + bindings
new Form({ fields }, { plugins, bindings });

// Separated mode + options
new Form({ values, labels, handlers, ... }, { options });

// Separated mode + validation + plugins
new Form({ values, labels, handlers, rules, ... }, { plugins });

// Full setup
new Form(
  { fields, values, labels, rules },
  { plugins, options, bindings, hooks },
);

All argument properties are optional and can be mixed freely.


Initialization Methods ​

When extending the Form class, you can use methods instead of constructor arguments:

javascript
import { Form } from 'mobx-formkit';

class MyForm extends Form {

  setup() {
    return { fields, values, labels /* same as first constructor arg */ };
  }

  plugins() {
    return { dvr: dvr({ package: validatorjs }) };
  }

  options() {
    return { validateOnChange: true };
  }

  bindings() {
    return { /* custom bindings */ };
  }

  hooks() {
    return { onSuccess(form) { /* ... */ } };
  }

  handlers() {
    return { onSubmit(e) { /* ... */ } };
  }
}

The object returned by each method is merged with the constructor argument during initialization, giving you flexible composition.

Released under the MIT License.