useField() hook
Concept
When using the useField
hook, you need to pass your component props
to the hook.
Then the hook gives you access to the field state.
Hooks methods
setValue()
Set the value of the field.
setValue
also accept a function that gives the previous value (like React setState
).
validating.start() & validating.end()
Allows to notify the field that some validations are done in the background (like retrieving some data).
This will update the isValidating
state at the field level and also at the form and step levels.
Notify the start of async processing with validating.start()
.
Notify the end of async processing with validating.end()
.
Hook values
value
Get the current value of the field.
valueDebounced
Get the current value of the field, but sync with the debouncing. It can be useful to determinate when to show error messages.
id
Return a unique id. This id will be created based on the form id and the field name.
isValid
Returns true
if the field is valid.
isValidating
Returns true
if the field is running async validation or after validating.start()
is called (until validation.end()
is called).
isPristine
Returns true
if the field has not been changed.
isSubmitted
Returns true
either if the current step or the form is submitted.
errorMessage
Returns the first error message.
errorMessages
Returns all error messages.
resetKey
Allows you to put a key on elements that you want to be reinitialize when the from is reset.
Can be useful for element like <input>
or <select>
.
Hook extra values
This values are not available in the useForm() fields
object.
otherProps
Get the props passed to the component without the Formiz props. Allows you to keep composition by spreading this object in your component.
Field props
When a component is created with the useField()
hook,
you can pass the following props to the component.
name *
Required
The name is required to register the field in the form.
Nested objects
The name
props can use lodash-like dot paths to reference nested values.
Arrays
The name
props allow also arrays and arrays of objects out of the box.
Using lodash-like bracket syntax for name
allow you to handle fields in a list.
debounce
Number of milliseconds for debouncing validations. (default is 100
).
defaultValue
Pass an initial value to the field.
keepValue
Set to true
to keep the value when the field is unmounted from the DOM. (default is false
).
formatValue(fieldValue)
Format the value before saving it into the internal state.
onChange(fieldValue)
Triggered everytime that setValue() is called inside the field.
required
Shortcut for isRequired() validation
validations
Accept an array of object with the following keys:
rule(fieldValue)
: Built in validation rule or custom validation function (must returnstrue
if the rule is valid).deps
: Array of external values used in the rule function (like array of dependencies in useEffect).message
: The message if the rule is invalid.
asyncValidations
Async validations allows you to run asynchronous code to validate a field, such as an API call.
asyncValidations
will only be triggered if all the others validations
rules are valid.
Accept an array of object with the following keys:
rule(fieldValue)
: Must returns a Promise that returnstrue
if the rule is valid.deps
: Array of external values used in the rule function (like array of dependencies in useEffect).message
: The message if the rule is invalid.
PropTypes validations
If you are using prop-types to document props in your project,
you can get fieldPropTypes
and fieldDefaultProps
from @formiz/core
to pass to your custom fields for PropTypes validations.