Forms API


Form.extend(prototypeProps[, constructorProps])

This is the entry point for defining your own forms.

Creates a new constructor which inherits from Form.

  • prototypeProps (Object) –

    form Fields and other prototype properties for the new form, such as a custom constructor and validation methods.

    See DeclarativeFieldsMeta() for details of how Fields passed as prototypeProps are handled.

  • constructorProps (Object) – properties to be set directly on the new constructor function.
class Form([kwargs])

A collection of Fields that knows how to validate and display itself.

  • kwargs (Object) – form options, which are as follows:
  • (Object) – input form data, where property names are field names. A form with data is considered to be “bound” and ready for use validating and coercing the given data.
  • kwargs.files (Object) – input file data.
  • kwargs.errors (ErrorObject) –

    initial errors to be displayed.


    Passing initial errors will prevent validation from firing if a form has input data and isValid() or errors() are called during rendering.

    This argument is intended for redisplaying a form with the user’s input and errors received from another source, such as an API call.

    This is more typical of server-side usage.

  • kwargs.validation

    Configures form-wide interactive validation when the user makes changes to form inputs in the browser. This can be a String, or an Object which configures default validation for form inputs.

    If 'manual', interactive validation will not be performed – you are responsible for hooking up validation and using methods such as setData() and isValid() to perform all validation. This is the default setting.

    If an Object is given, it should have the following properties:

    The name of the default event to use to trigger validation. For example, if 'blur', text input validation will be performed when the input loses focus after editing. Multiple, space-separated event names can be given.
    A delay, in milliseconds, to be used to debounce performing of onChange validation.

    If 'auto', validation behaviour will be the equivalent of having passed:

    ..code-block: javascript

    validation: {on: ‘blur change’, onChangeDelay: 369}

    If any String but 'manual' or 'auto' is given, it will be used as if it were passed as the on property of an Object.

    For example, passing {validation: 'change'} will cause form inputs to trigger validation as soon as the user makes any change.

    New in version 0.6.

  • kwargs.controlled (Boolean) –

    Configures whether or not the form will render controlled components - when using controlled components, you can update the values displayed in the form after its initial render using form.setData() or form.updateData()

    New in version 0.6.

  • kwargs.onChange (Function) –

    If interactive validation is configured for a Form or any of its Fields, this callback function must be provided, or an Error will be thrown.

    It will be called any time the form’s input data or validation state changes as the result of user input.

    Typically, this function should at least force React to update the component in which the Form is being rendered, to display the latest validation state to the user from the last change they made to the form.

    New in version 0.9: Replaces kwargs.onStateChange

  • kwargs.autoId (String) – a template for use when automatically generating id attributes for fields, which should contain a {name} placeholder for the field name – defaults to id_{name}.
  • kwargs.prefix (String) – a prefix to be applied to the name of each field in this instance of the form - using a prefix allows you to easily work with multiple instances of the same Form object in the same HTML <form>, or to safely mix Form objects which have fields with the same names.
  • kwargs.initial (Object) – initial form data, where property names are field names – if a field’s value is not specified in data, these values will be used when initially rendering field widgets.
  • kwargs.errorConstructor (Function) – the constructor function to be used when creating error details. Defaults to ErrorList().
  • kwargs.labelSuffix (String) – a suffix to be used when generating labels in one of the convenience methods which renders the entire Form – defaults to ':'.
  • kwargs.emptyPermitted (Boolean) – if true, the form is allowed to be empty – defaults to false.

Prototype Properties


This string defines the format used to generate name attributes for fields when a form instance is given a prefix. It must contain {prefix} and {name} placeholders.

The default format is '{prefix}-{name}'.

Type String:

Instance Properties

Form options documented in kwargs above are all set as instance properties.

The following instance properties are also available:


Form fields for this instance of the form.

Since a particular instance might want to alter its fields based on data passed to its constructor, fields given as part of the form definition are deep-copied into fields every time a new instance is created.

Instances should only ever modify fields.


fields does not exist until the Form constructor has been called on the form instance that’s being constructed.

This is important to note when you intend to dynamically modify fields when extending a form – you must call the constructor of the form which has been extended before attempting to modify fields.

Type:Object with field names as property names and Field instances as properties.

Determines if this form has been given input data which can be validated.

true if the form has data or files set.


After a form has been validated, it will have a cleanedData property. If your data does not validate, cleanedData will contain only the valid fields.

Type:Object with field names as property names and valid, cleaned values coerced to the appropriate JavaScript type as properties.

Prototype Functions

Validation: Methods for validating and getting information about the results of validation:

Form#validate([form[, callback(err, isValid, cleanedData)]])

Forces the form to revalidate from scratch. If a <form> is given, data from it will be set on this form first. Otherwise, validation will be done with this form’s current input data.

  • form – a <form> DOM node – if React’s representation of the <form> is given, its getDOMNode() function will be called to get the real DOM node.
  • Boolean, Object) callback (function(Error,) –

    Callback for asynchronous validation.

    This argument is required if the form uses asynchronous validation - an Error will be thrown if it’s not given in this case.

    The callback should be an errback with the signature (err, isValid, cleanedData).


true if the form only has synchronous validation and is valid.

New in version 0.6.

Changed in version 0.10: Added callback argument for async validation.


Validates and cleans and populates errors and cleanedData.

You shouldn’t need to call this function directly in general use, as it’s called for you when necessary by Form#isValid() and Form#errors().


Validates and cleans for the given field names and triggers cross-form cleaning in case any form.cleanedData it uses has changed.

  • fieldNames (Array) – a list of unprefixed field names.
Form#clean([callback(err, validationError)])

Hook for doing any extra form-wide cleaning after each Field’s Field#clean() has been called. Any ValidationError() thrown by this method will not be associated with a particular field; it will have a special-case association with the field named '__all__'.

  • String|ValidationError) callback (function(Error,) – Optional callback for asynchronous validation.

Changed in version 0.10: This method can now be defined with a callback parameter if it needs to perform async validation. The form will provide a callback function and wait for it to be called before finishing validation.

Data mutability: Methods for programmatically changing the form’s data.


Resets the form to its initial render state, optionally giving it new initial data.

  • initialData (Object) – new initial data for the form.

New in version 0.6.

Form#setData(data[, kwargs])

Replaces the form’s with the given data (and flips form.isInitialRender to false, if necessary) and triggers form cleaning and validation, returning the result of form.isValid().

  • data (Object) – new input data for the form
  • kwargs (Object) – data updating options, which are as follows:
  • kwargs.prefixed (Boolean) –

    pass true when updating data in a prefixed form and the field names in data are already prefixed – defaults to false

    New in version 0.6.


true if the form has no errors after validating the updated data, false otherwise.

New in version 0.5.


Replaces with form’s input data with data extracted from a <form> (i.e. with formData()).

When using multiple forms with prefixes, form data will always be prefixed - using this method when working with manually extracted form data should ensure there are no surprises if moving from non-prefixed forms to prefixed forms.

  • formData (Object) –
    new input data for the form, which has been extracted from a <form>

    New in version 0.6.

Form#updateData(data[, kwargs])

Updates the form’s (and flips form.isInitialRender to false, if necessary).

By default, triggers validation of fields which had their input data updated, as well as form-wide cleaning.

  • data (Object) –

    partial input data for the form, field name -> input data.

    If your form has a prefix, field names in the given data object must also be prefixed.

  • kwargs (Object) – data updating options, which are as follows:
  • kwargs.prefixed (Boolean) – pass true when updating data in a prefixed form and the field names in data are already prefixed – defaults to false

The follwing options are intended for use with controlled forms, when you’re only updating data in order to change what’s displayed in the controlled components:

  • kwargs.validate (Boolean) – pass false if you want to skip validating the updated fields – defaults to true. This can be ignored if you’re passing known-good data.
  • kwargs.clearValidation (Boolean) – pass false if you’re skipping validation and you also want to skip clearing of the results of any previous validation on the fields being updated, such as error messages and cleanedData – defaults to true

New in version 0.6.

BoundFields: Methods which create BoundField helpers for rendering the form’s fields.


Creates a BoundField() for each field in the form, in the order in which the fields were created.

  • test (Function(field,name)) – If provided, this function will be called with field and name arguments - BoundFields will only be generated for fields for which true is returned.

A version of Form#boundFields() which returns an Object with field names as property names and BoundFields as properties.


Creates a BoundField() for the field with the given name.

  • name (String) – the name of a field in the form.
Returns:a list of BoundField() objects that correspond to hidden fields. Useful for manual form layout.
Returns:a list of BoundField() objects that do not correspond to hidden fields. The opposite of the Form#hiddenFields() function.

Error: Methods for wokring with the form’s validation errors.

Form#addError(field, error)

This function allows adding errors to specific fields from within the form.clean() method, or from outside the form altogether.

  • field (String) – the name of the field to which the error(s) should be added. If its value is null the error will be treated as a non-field error as returned by form.nonFieldErrors().
  • error (String|Array|ValidationError|Object) –

    the error argument can be a single error, a list of errors, or an object that maps field names to lists of errors. A single error can be given as a String or an instance of a ValidationError().

    Multiple errors can be given as an Array, an Object which maps field names to validation errors, or a ValidationError created with an Array or Object.

If the error argument is an Object, the field argument must be null – errors will be added to the fields that correspond to the properties of the object.


Using form.addError() automatically removes the relevant field from form.cleanedData.

New in version 0.5.

Changed in version 0.10: addErrpr() will no longer add a duplicated error message for the same field. This can happen if event-based validation which runs repeatedly adds errors to a field other than that which triggered the validation, such as in a custom clean() method.


Getter for validation errors which first cleans the form if there are no errors defined yet.

Returns:validation errors for the form, as an ErrorObject()
Returns:errors that aren’t associated with a particular field - i.e., errors generated by Form#clean(), or by calling Form#addError() and passing null instead of a field name. Will be an empty error list object if there are none.

This method’s intended use is replacing a Form’s errors with those received from another source, such as an API call which performs additional validation.


Changes: methods for working with changed data.

Returns:a list of the names of fields which have differences between their initial and currently bound values.
Returns:true if data differs from initial, false otherwise.

Status: methods for determining the form’s status:

Returns:true if the form’s prototype defines any custom cleaning methods which have an arity of 1 (which is assumed to mean they have defined an async callback parameter).

New in version 0.10.


Determines whether or not the form has valid input data for all required fields. This can be used to indicate to the user that a form which is being validated as they fill it in is ready for submission.

A form which has any errors or is pending async validation will not be considered complete.

The distinction between isComplete() and Form#isValid() is that a form which has had, for example, a single field filled in and validated is valid according to the partial validation which has been performed so far (i.e. it doesn’t have any error messages) but isn’t yet complete.

New in version 0.6.

Changed in version 0.10: A form which isPending() will not be considered complete.


Determines if the form needs to be multipart-encoded in other words, if it has a FileInput().

Returns:true if the form needs to be multipart-encoded.
Returns:true if true if the form is waiting for async validation to complete.

Determines whether or not the form has errors, triggering cleaning of the form first if necessary.

When user input is being incrementally validated as it’s given, this function gives you the current state of validation (i.e. whether or not there are any errors). It will not reflect the validity of the whole form until a method which performs whole-form validation (Form#validate() or setData()) has been called.

Returns:true if the form is has input data and has no errors, false otherwise. If errors are being ignored, returns false.
Returns:true` if the form is waiting for async validation of its clean(callback) method to complete.

New in version 0.10.


Determines if a form which is an extra form in a FormSet has changed from its initial values. Extra forms are allowed to be empty, so required fields in them do not become truly required until the form has been modified.

Returns:true if a form has emptyPermitted and has changed from its initial values.

New in version 0.9.

Prefixes: Methods for working with form prefixes.

Returns:the given field name with a prefix added, if this Form has a prefix.

Adds an initial prefix for checking dynamic initial values.

Returns:the given field name with a prefix-size chunk chopped off the start if this form has a prefix set and the field name starts with it.



This mixin function is responsible for setting up form fields when a new Form constructor is being created.

It pops any Fields it finds off the form’s prototype properties object, determines if any forms are also being mixed-in via a __mixins__ property and handles inheritance of Fields from any form which is being directly extended, such that fields will be given the following order of precedence should there be a naming conflict with any of these three sources:

  1. Fields specified in prototypeProps
  2. Fields from a mixed-in form
  3. Fields from the Form being inherited from

If multiple forms are provided via __mixins__, they will be processed from left to right in order of precedence for mixing in fields and prototype properties.

Forms can prevent fields from being inherited or mixed in by adding a same-named property to their prototype, which isn’t a Field. It’s suggested that you use null as the value when shadowing to make this intent more explicit.

  • Form (Form) – a Form constructor

true if the given Form constructor’s prototype defines any custom cleaning methods which have an arity of 1 (which is assumed to mean they have defined an async callback parameter).