useForm
Main hook to create a form instance.
Type#
FormConfig#
Configuration to pass to useForm()
initialValues#
The initial values of the form. You can pass directly the values or a function that returns the values.
validateOnChange#
If true, the form will be validated every time any field changes. By default it is true.
validateOnBlur#
If true, the form will be validated every time a blur event occurs. By default it is false.
onValidate#
Callback 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.
validateDebounce#
Value 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 for- wait(300) and- leading(false)
- a number: debounce will be active withwaitas the number you passed, andleadingfalse
- a { wait?: number; leading?: boolean }object: debounce will be active with the values you passed
onSubmit#
Callback 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.
onFailedSubmit#
Callback 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.
UseFormResult#
It is what useForm() returns. It is a Form instance with two additional components: <Form/> and <FormContext/>.
Form#
It's the form instance.
values#
The 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().
observableValues#
The 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.
errors#
The 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.
touched#
The 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.
isSubmitting#
true if the form is submitting.
isValidating#
true if the form is validating.
isDirty#
true if the values are different from the initialValues.
isValid#
true if there are errors.
setErrors#
Sets 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.
setTouched#
Sets 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.
setValues#
Sets the values of the form.
setFieldValue#
Sets the value of a field.
setFieldError#
Sets the error of a field.
addFieldError#
Adds an error to a field. It will be merged with the current error of the field.
setFieldTouched#
Sets if a field should be considered touched.
getFieldValue#
Gets the value of a field.
getFieldError#
Gets the first error of a field, if any.
getFieldErrors#
Gets the errors of a field, if any.
isFieldTouched#
Returns true if a field has been touched by the user.
isFieldValid#
Returns true if the field has no error.
isFieldDirty#
Returns true if the field has a different value than the initial value
validate#
Performs form validation. It returns a Promise that is resolved with the errors.
validateField#
Performs field validation. It returns a Promise that is resolved with the errors of the field.
reset#
Resets the form values to the initial values. If you pass a value as argument, that value will be used as initial values.
resetField#
Resets 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.
submit#
Performs form submission. It returns a Promise that is resolved with the errors of the form.
handleSubmit#
Similar to submit but it can receive a submit event that will be default prevented.
handleReset#
Similar to reset but it can receive a reset event that will be default prevented.
register#
Used internally by useField in order to register itself to the form.
freeze#
It freezes the form values, preventing them to be changed: reset, resetField, setFieldValue and setValues methods do nothing when the form is freezed.
unfreeze#
It unfreezes the form values.
isFreezed#
Whether the form is freezed.