docs/api/Selectors.md
redux-form provides a set of useful Redux state
selectors that may be used in
any part of your application to query the state of any of your forms.
All of the selectors listed below have the same usage pattern: they all (apart from getFormNames) take the name of the form, and create a selector for whatever form state the selector is for.
They also all take an undocumented final parameter getFormState() that is
used to select the mount point of the redux-form reducer from the root Redux reducer (it
defaults to state => state.form, assuming that you have mounted the redux-form reducer under
form.
import {
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormMeta,
getFormAsyncErrors,
getFormSyncWarnings,
getFormSubmitErrors,
getFormError,
getFormNames,
isDirty,
isPristine,
isValid,
isInvalid,
isSubmitting,
hasSubmitSucceeded,
hasSubmitFailed
} from 'redux-form'
MyComponent = connect(state => ({
formValues: getFormValues('myForm')(state),
initialValues: getFormInitialValues('myForm')(state),
formSyncErrors: getFormSyncErrors('myForm')(state),
fields: getFormMeta('myForm')(state),
formAsyncErrors: getFormAsyncErrors('myForm')(state),
syncWarnings: getFormSyncWarnings('myForm')(state),
submitErrors: getFormSubmitErrors('myForm')(state),
formError: getFormError('myForm')(state),
names: getFormNames()(state),
dirty: isDirty('myForm')(state),
pristine: isPristine('myForm')(state),
valid: isValid('myForm')(state),
invalid: isInvalid('myForm')(state),
submitting: isSubmitting('myForm')(state),
submitSucceeded: hasSubmitSucceeded('myForm')(state),
submitFailed: hasSubmitFailed('myForm')(state)
}))(MyComponent)
getFormValues(formName:String) returns (state) => formValues:ObjectGets the current form values in real-time.
getFormInitialValues(formName:String) returns (state) => formInitialValues:ObjectGets the form's initial values.
getFormError(formName:String) returns (state) => formError:anyReturns the form-wide error, the one set with the special
_errorproperty.
getFormSyncErrors(formName:String) returns (state) => formSyncErrors:ObjectReturns the form synchronous validation errors.
getFormMeta(formName:String) returns (state) => formMeta:ObjectReturns the form's fields meta data, namely
touchedandvisited.
NOTE: redux-form creates the
formMetaobject lazily as each individual form field gets visited/touched/etc. Empty/missing properties imply that the corresponding field (or set of fields) has neither been visited nor touched.
getFormAsyncErrors(formName:String) returns (state) => formAsyncErrors:ObjectReturns the form asynchronous validation errors.
getFormSyncWarnings(formName:String) returns (state) => formSyncWarnings:ObjectReturns the form synchronous warnings.
getFormSubmitErrors(formName:String) returns (state) => formSubmitErrors:ObjectReturns the form submit validation errors.
getFormNames() returns (state) => formNames:ArrayGets the names of all the forms currently managed by Redux-Form.
The reason that this is a function that returns a function is twofold:
- symmetry with the other selectors
- to allow for the
getFormStateparameter described at the top of this page.
If you are using ImmutableJS, it will return a
List.
isDirty(formName:String) returns (state, ...fields:String[]) => dirty:booleanReturns
trueif the form is dirty, i.e. the values have been altered from the originalinitialValuesprovided. The opposite ofisPristine.
isPristine(formName:String) returns (state, ...fields:String[]) => pristine:booleanReturns
trueif the form is pristine, i.e. the values have NOT been altered from the originalinitialValuesprovided. The opposite ofisDirty.
isValid(formName:String) returns (state) => valid:booleanReturns
trueif the form is valid, i.e. has no sync, async, or submission errors. The opposite ofisInvalid.
isInvalid(formName:String) returns (state) => invalid:booleanReturns
trueif the form is invalid, i.e. has sync, async, or submission errors. The opposite ofisValid.
isSubmitting(formName:String) returns (state) => submitting:booleanReturns
trueif the form is submitting.
hasSubmitSucceeded(formName:String) returns (state) => submitSucceeded:booleanReturns
trueif the form has previously been successfully submitted.
hasSubmitFailed(formName:String) returns (state) => submitFailed:booleanReturns
trueif the form has previously failed to submit.