Skip to content

Form Options


Options Reference

All options can also be set on individual fields via the field options prop.

Validation Triggers

Control when validation runs.

OptionTypeDefaultDescription
validateOnInitbooleantrueValidate the entire form on initialization
validateOnSubmitbooleantrueValidate on submit. When disabled, onSuccess/onError hooks won't fire
validateOnBlurbooleantrueValidate field when it loses focus
validateOnChangebooleanfalseValidate field on every value change
validateOnChangeAfterSubmitbooleanfalseValidate on change only after the first form submission
validateOnChangeAfterInitialBlurbooleanfalseValidate on change only after the field has been blurred at least once
validateOnClearbooleanfalseValidate after clear()
validateOnResetbooleanfalseValidate after reset()
validateDeletedFieldsbooleanfalseInclude soft-deleted fields in validation
validateDisabledFieldsbooleanfalseInclude disabled fields in validation
validatePristineFieldsbooleantrueInclude untouched fields in validation
validateTrimmedValuebooleanfalseApply trim() to field value before validating

💡 Tip: Use validateOnChange: true for real-time feedback, validateOnBlur: true (default) for less intrusive validation. Combine validateOnChangeAfterInitialBlur for a balance — start validating on change only after the user has left the field once.

javascript
// Validate on every keystroke after first blur
const options = {
  validateOnChange: false,
  validateOnChangeAfterInitialBlur: true,
  showErrorsOnChange: true,
};

Error Display

Control when and how error messages are shown.

OptionTypeDefaultDescription
showErrorsOnInitbooleanfalseShow errors on form init (with validateOnInit)
showErrorsOnSubmitbooleantrueShow errors after submit
showErrorsOnBlurbooleantrueShow errors on field blur
showErrorsOnChangebooleantrueShow errors on value change
showErrorsOnClearbooleanfalseShow errors after clear()
showErrorsOnResetbooleantrueShow errors after reset()
defaultGenericErrorstringnullGeneric error message shown when validation fails
submitThrowsErrorbooleantrueIf true, submit() throws when validation fails (requires defaultGenericError)
bubbleUpErrorMessagesbooleanfalseContainer fields show the first child error instead of null
javascript
// Generic error message that doesn't throw
const options = {
  defaultGenericError: 'Please fix the errors before submitting.',
  submitThrowsError: false,
};
javascript
// Show first child error on container fields
const options = {
  bubbleUpErrorMessages: true,
};
// form.$('address').error → returns first error from address.street or address.city

Strict Mode

Throw errors when referencing undefined fields — useful for catching bugs during development.

OptionTypeDefaultDescription
strictSelectbooleantrueThrow if selecting an undefined field via select() or $()
strictSetbooleanfalseThrow if setting a prop on an undefined field via set()
strictUpdatebooleanfalseThrow if updating an undefined field via update()
strictDeletebooleantrueThrow if deleting an undefined field via del()
javascript
// Lax mode — no errors for undefined fields
const options = {
  strictSelect: false,
  strictSet: false,
  strictUpdate: false,
  strictDelete: false,
};

Field Retrieval

Filter which field values, errors, or properties are returned by get(), values(), errors(), etc.

OptionTypeDefaultDescription
retrieveOnlyEnabledFieldsValuesbooleanfalseExclude disabled field values from get('value') / values()
retrieveOnlyEnabledFieldsErrorsbooleanfalseExclude disabled field errors from get('error') / errors()
retrieveOnlyDirtyFieldsValuesbooleanfalseInclude only changed field values in get('value') / values()
removeNullishValuesInArraysbooleanfalseRemove null, undefined, "" from array values
retrieveNullifiedEmptyStringsbooleanfalseConvert empty strings to null in retrieved values
softDeletebooleanfalsedel() marks fields as deleted instead of removing them
preserveDeletedFieldsValuesbooleanfalseWhen re-adding a soft-deleted field, preserve its original values
javascript
// Get only changed, non-disabled values
const options = {
  retrieveOnlyEnabledFieldsValues: true,
  retrieveOnlyDirtyFieldsValues: true,
};

// Remove nulls and empty strings from arrays
const options = {
  removeNullishValuesInArrays: true,  // [1, null, 3, ""] → [1, 3]
};

// Soft delete with preserved values
const options = {
  softDelete: true,
  preserveDeletedFieldsValues: true,
};

Converters & Values

Control how input converters and value formatting behave.

OptionTypeDefaultDescription
applyInputConverterOnInitbooleantrueApply input converter on field creation
applyInputConverterOnSetbooleantrueApply input converter on set()
applyInputConverterOnUpdatebooleantrueApply input converter on update()
autoTrimValuebooleanfalseAutomatically trim whitespace from string values on every change
autoParseNumbersbooleanfalseParse string input to number when the field's initial value is a number
fallbackValueany""Default value when a field is cleared or created without an explicit value
uniqueIdfunctionCustom function to generate field IDs (field => string). Useful for SSR hydration
javascript
// Auto-trim and auto-parse
const options = {
  autoTrimValue: true,         // "  hello  " → "hello"
  autoParseNumbers: true,      // "20" → 20 (when initial value is a number)
};

// Custom fallback value
const options = {
  fallbackValue: null,         // default is "", now clear() sets null
};

// Deterministic IDs for SSR
const options = {
  uniqueId: (field) => `${field.path}-${Date.now()}`,
};

Debounce & Plugin Order

Control validation timing and plugin execution order.

OptionTypeDefaultDescription
validationDebounceWaitnumber250Milliseconds to delay validation after the last change
validationDebounceOptionsobject{ leading: false, trailing: true }Lodash debounce options
validationPluginsOrderstring[]undefinedOrder of validation plugins: vjf, dvr, svk, yup, zod, joi
stopValidationOnErrorbooleanfalseStop validating a field once it's already marked invalid
resetValidationBeforeValidatebooleantrueReset validation state before each validate() call
javascript
// Debounced validation — useful for search-as-you-type
const options = {
  validateOnChange: true,
  validationDebounceWait: 500,
  validationDebounceOptions: {
    leading: false,
    trailing: true,
    maxWait: 2000,   // force validation at most every 2s
  },
};

// Run VJF first, then DVR, stop on first error
const options = {
  validationPluginsOrder: ['vjf', 'dvr', 'svk'],
  stopValidationOnError: true,
};

Other

OptionTypeDefaultDescription
fallbackbooleantrueControls whether fields and their props can be resolved outside the strict struct/fields definition. When true: fields are created from definition data even if not in the struct; values with keys not in fields are included as field data. When false: fields are created only if they exist in the struct, and only explicitly defined field keys receive values. See Mixed Mode.

How to Set Options

Set Options On Form Constructor

javascript
import { Form } from 'mobx-react-form';

const options = {
  showErrorsOnInit: true,
  validateOnChange: true,
  autoParseNumbers: false,
};

const form = new Form({ fields }, { options });

Set Options On Extended Form Class

Use the options() method to return your options object:

javascript
import { Form } from 'mobx-react-form';

class MyForm extends Form {
  options() {
    return {
      showErrorsOnInit: true,
      validateOnChange: true,
    };
  }
}

The object returned from options() is merged with any options passed to the constructor.

Set Options After Form Initialization

javascript
form.state.options.set({
  validateOnInit: false,
  validateOnChange: false,
  strictUpdate: true,
});

Get Current Form Options

javascript
const allOptions = form.state.options.get();
// => { validateOnInit: false, validateOnChange: true, ... }

Get Form Option by Key

javascript
const value = form.state.options.get('showErrorsOnInit');
// => true

💡 All options can also be set per-field via the field options prop:

javascript
const fields = {
  email: {
    label: 'Email',
    options: {
      validateOnChange: true,     // validate this field on every keystroke
      showErrorsOnChange: true,   // show errors immediately
    },
  },
};

Per-field options override form-level options.

Released under the MIT License.