Use this hook to access values and methods from a <Formiz>
component.
Usage from outside <Formiz>
# 1. Connect your form# Copy import React from 'react'
import { Formiz , useForm } from '@formiz/core'
import { MyField } from './MyField'
export const MyForm = ( ) => {
const myForm = useForm ( )
return (
< Formiz connect = { myForm } >
{ }
</ Formiz >
)
}
2. Access values and methods# You have now access to the form.
Copy import React from 'react'
import { Formiz , useForm } from '@formiz/core'
import { MyField } from './MyField'
export const MyForm = ( ) => {
const myForm = useForm ( )
return (
< Formiz connect = { myForm } >
< form onSubmit = { myForm . submit } >
{ myForm . isValid && 'The form is valid!' }
{ }
< button type = " submit " >
Submit
</ button >
< form >
</ Formiz >
)
}
Usage from a child component of <Formiz>
# Copy import React from 'react'
import { Formiz , useForm } from '@formiz/core'
import { MyField } from './MyField'
export const MySubComponent = ( ) => {
const myForm = useForm ( )
return myForm . isValid && 'The form is valid!' ;
}
export const MyForm = ( ) => {
return (
< Formiz >
...
< MySubComponent />
...
</ Formiz >
)
}
Hook options# subscribe# TL;DR Use this option for performance optimization only
The subscribe
option allows you to tell which part of the internal state you want to subscribe to.
By default, you subscribe to the complete internal state. But for performance optimizations, you can choose exactly what you want.
Each time the internal state is updated, the component with the useForm()
is updated.
With this you can avoid many unecessary rerender and optimize your application by choosing only what you want to subscribe to.
Available values# Copy
useForm ( )
useForm ( { subscribe : true } )
useForm ( { subscribe : { form : true , fields : true } } )
useForm ( { subscribe : false } )
useForm ( { subscribe : { form : false , fields : false } } )
useForm ( { subscribe : 'form' } )
useForm ( { subscribe : { form : true } } )
useForm ( { subscribe : { form : true , fields : false } } )
useForm ( { subscribe : 'fields' } )
useForm ( { subscribe : { fields : true } } )
useForm ( { subscribe : { form : false , fields : true } } )
useForm ( { subscribe : { form : true , fields : [ 'email' , 'password' ] } } )
useForm ( { subscribe : { fields : [ 'email' , 'password' ] } } )
Subscribe to fields with nested objects and arrays# Imagine the follwing form:
Copy < Formiz >
< Field name = " something.collection[0].name " />
< Field name = " something.collection[0].email " />
< Field name = " something.collection[1].name " />
< Field name = " something.collection[1].email " />
< Field name = " something.else " />
</ Formiz >
You can subscribe like that:
Copy useForm ( { subscribe : { fields : [ 'something.collection[1].name' ] } } ) ;
useForm ( { subscribe : { fields : [ 'something.collection[1]' ] } } ) ;
useForm ( { subscribe : { fields : [ 'something.collection' ] } } ) ;
useForm ( { subscribe : { fields : [ 'something' ] } } ) ;
Form actions# The useForm
return an object with the following properties:
submit()# Allow you to submit the form
Copy < Formiz connect = { myForm } >
< form onSubmit = { myForm . submit } >
{ }
< button type = " submit " >
Submit
</ button >
</ form >
</ Formiz >
submitStep()# Allows you to submit the current step.
Copy < Formiz connect = { myForm } >
< form onSubmit = { myForm . submitStep } >
< FormizStep name = " step1 " >
{ }
</ FormizStep >
< FormizStep name = " step2 " >
{ }
</ FormizStep >
< button type = " submit " >
Submit
</ button >
</ form >
</ Formiz >
invalidateFields(objectOfErrors)# Allow you to invalidate one or many fields.
Useful for API errors or other external errors.
Copy invalidateFields ( {
'fieldName' : 'My error message' ,
'secondFieldName' : 'Another error message' ,
} )
setFieldsValues(objectOfValues, options)# Allow you to change the value of one or many fields imperatively.
Useful to change one or many values based on an external action.
โ ๏ธ Don't use undefined
as new value, it will not trigger the update (use null
instead).
Copy
setFieldsValues ( {
fieldName : 'New value' ,
nested : {
subField : 'New value' ,
} ,
} )
setFieldsValues ( {
fieldName : 'New value' ,
'nested.subField' : 'New value' ,
} )
Options# keepUnmounted# Set to true
to keep the values of unmounted fields.
The values will be used as initial values on the next mount of each fields.
(default is false
).
Copy
< FieldInput
name = " fieldA "
label = " Field A "
/>
{ form . values ?. fieldA === 'hello' && (
< FieldInput
name = " fieldB "
label = " Field B "
/>
) }
Copy form . setFieldsValues (
{ fieldA : 'hello' , fieldB : 'world' } ,
{ keepUnmounted : true }
)
keepPristine# Set to false
to change the isPristine
state of updated fields.
(default is true
).
Copy form . setFieldsValues (
{ fieldA : 'hello' , fieldB : 'world' } ,
{ keepPristine : false }
)
reset(options)# Allows to reset the form with all fields values to their defaultValue.
Copy const myForm = useForm ( )
myForm . reset ( )
Options# Available reset elements for the only
and exclude
options.
Copy 'pristine'
'submitted'
'validating'
'resetKey'
'currentStep'
'visited'
'values'
only# Allows to reset only some elements.
Copy
form . reset ( { only : [ 'pristine' , 'submitted' ] ย } ) ;
exclude# Allows to prevent reseting some elements.
Copy
form . reset ( { exclude : [ 'values' ] ย } ) ;
getFieldStepName(fieldName)# Allows you to get the step name of a field.
Copy const myForm = useForm ( )
const stepNameOfEmail = myForm . getFieldStepName ( 'email' )
myForm . goToStep ( stepNameOfEmail )
nextStep()# Move to the next step.
Copy const myForm = useForm ( )
myForm . nextStep ( )
prevStep()# Move to the previous step.
Copy const myForm = useForm ( )
myForm . prevStep ( )
goToStep(name)# Go to a specific step.
Copy const myForm = useForm ( )
myForm . goToStep ( 'my-step' )
Form state# isValid# Returns true if the form is valid.
Copy const myForm = useForm ( )
myForm . isValid
isValidating# Returns true if one field or more is running async validations.
Copy const myForm = useForm ( )
myForm . isValidating
isSubmitted# Returns true if the form is submitted.
Copy const myForm = useForm ( )
myForm . isSubmitted
resetKey# Allows you to reset some internal state when the form is reset.
Copy const myForm = useForm ( )
useEffect ( ( ) => {
} , [ form . resetKey ] )
currentStep# Returns the current step object.
Copy
{
index : number ;
name : string ;
label ? : React . ReactNode ;
isCurrent : boolean ;
isPristine : boolean ;
isValid : boolean ;
isValidating : boolean ;
isVisited : boolean ;
isSubmitted : boolean ;
}
Copy const myForm = useForm ( )
myForm . currentStep ?. name
steps# Returns an array with all the steps of the form.
Copy
{
index : number ;
name : string ;
label ? : React . ReactNode ;
isCurrent : boolean ;
isPristine : boolean ;
isValid : boolean ;
isValidating : boolean ;
isVisited : boolean ;
isSubmitted : boolean ;
}
Copy const myForm = useForm ( )
return (
< ul >
{ myForm . steps ?. map ( ( step ) => (
< li key = { step . name } >
{ step . isValid ? 'โ
' : 'โ' }
{ ' ' }
{ step . label || step . name }
</ li >
) ) }
</ ul >
)
isStepPristine# Returns true if all the fields are pristine on the current step.
Copy const myForm = useForm ( )
myForm . isStepPristine
isStepValid# Returns true if all the fields are valid on the current step.
Copy const myForm = useForm ( )
myForm . isStepValid
isStepValidating# Returns true if one field or more is running async validations on the current step.
Copy const myForm = useForm ( )
myForm . isStepValidating
isStepSubmitted# Returns true if the current step is submitted.
Copy const myForm = useForm ( )
myForm . isStepSubmitted
isFirstStep# Returns true if the current step is the first step.
Copy const myForm = useForm ( )
myForm . isFirstStep
isLastStep# Returns true if the current step is the last step.
Copy const myForm = useForm ( )
myForm . isLastStep
Fields state# values# Returns an object with all the values of the form with nested values .
See the name
props for fields for nested objects and array values.
Copy const myForm = useForm ( )
myForm . values
flatValues# Returns an object with all the values of the form without nested values .
Copy const myForm = useForm ( )
myForm . flatValues
fields# Returns an object with all the fields of the form with their hook values.
See the name
props for fields for nested objects and array syntaxe.
See the useField() hook values for available values.
Copy const myForm = useForm ( )
myForm . fields
myForm . fields ?. fieldA ?. isValid
myForm . fields ?. nested ?. fieldB ?. errorMessage
myForm . fields ?. collection ?. [ 0 ] ?. field ?. isPristine