useForm
Main hook to create a form instance.
#
Type#
FormConfigConfiguration to pass to useForm()
#
initialValuesThe initial values of the form. You can pass directly the values or a function that returns the values.
#
validateOnChangeIf true
, the form will be validated every time any field changes. By default it is true
.
#
validateOnBlurIf true
, the form will be validated every time a blur
event occurs. By default it is false
.
#
onValidateCallback that is called to validate the form. It receives the values of the form and it should return an object whose keys are the field names (with dot notation) and the value is string | string[] | undefined
. It can be an async function.
#
validateDebounceValue that tells if validation debounce should be active and how:
false
: debounce will not be active (as per default)true
: debounce will be active, with default values forwait
(300) andleading
(false)- a
number
: debounce will be active withwait
as the number you passed, andleading
false - a
{ wait?: number; leading?: boolean }
object: debounce will be active with the values you passed
#
onSubmitCallback that is called when submitting the form. It receives the values of the form and can return form errors as onValidate. If it returns an errors object, these errors will be set to the form.
#
onFailedSubmitCallback that is called when trying to submit the form but there are validation errors that prevents the submission to continue. It gives the chance to show a notification to the user saying that there are errors that should be solved.
#
UseFormResultIt is what useForm()
returns. It is a Form
instance with two additional components: <Form/>
and <FormContext/>
.
#
FormIt's the form instance.
#
valuesThe current values of the form. Its reference changes whenever a value of the form changes, even in deep nested values. Therefore, it can be used as dependency in useEffect()
.
#
observableValuesThe mobx-observable current values of the form. It has a stable reference, even if the values change. Therefore, don't use it as a dependency.
#
errorsThe current errors of the form. It's an object whose keys are the field names (with dot notation) and the value is string | string[] | undefined
.
#
touchedThe current fields touched by the user. It's an object whose keys are the field names (with dot notation) and the value is boolean | undefined
.
#
isSubmittingtrue
if the form is submitting.
#
isValidatingtrue
if the form is validating.
#
isDirtytrue
if the values are different from the initialValues.
#
isValidtrue
if there are errors.
#
setErrorsSets the errors of the form. errors
should be an object whose keys are the field names (with dot notation) and the value is string | string[] | undefined
.
#
setTouchedSets the touche fields of the form. touched
should be an object whose keys are the field names (with dot notation) and the value is boolean | undefined
.
#
setValuesSets the values of the form.
#
setFieldValueSets the value of a field.
#
setFieldErrorSets the error of a field.
#
addFieldErrorAdds an error to a field. It will be merged with the current error of the field.
#
setFieldTouchedSets if a field should be considered touched.
#
getFieldValueGets the value of a field.
#
getFieldErrorGets the first error of a field, if any.
#
getFieldErrorsGets the errors of a field, if any.
#
isFieldTouchedReturns true
if a field has been touched by the user.
#
isFieldValidReturns true
if the field has no error.
#
isFieldDirtyReturns true
if the field has a different value than the initial value
#
validatePerforms form validation. It returns a Promise that is resolved with the errors.
#
validateFieldPerforms field validation. It returns a Promise that is resolved with the errors of the field.
#
resetResets the form values to the initial values. If you pass a value as argument, that value will be used as initial values.
#
resetFieldResets the value of the field to the initial value. If you pass a value as second argument, that value will be used as initial value.
#
submitPerforms form submission. It returns a Promise that is resolved with the errors of the form.
#
handleSubmitSimilar to submit but it can receive a submit event that will be default prevented.
#
handleResetSimilar to reset but it can receive a reset event that will be default prevented.
#
registerUsed internally by useField
in order to register itself to the form.
#
freezeIt freezes the form values, preventing them to be changed: reset
, resetField
, setFieldValue
and setValues
methods do nothing when the form is freezed.
#
unfreezeIt unfreezes the form values.
#
isFreezedWhether the form is freezed.