Example Source Code
const {isValid, validationError, validationErrorOriginalResult, formData, setPathValue,
unsetPathValue, setFormIsSubmitted, setPathIsBlurred, unsetPathIsBlurred, setFormData,
setRules, setFormDataAndRules, resetForm, getValue, getError, forceRunAllValidations } =
useValidatableForm({
rules,
initialFormData,
hideBeforeSubmit,
showAfterBlur,
focusToErrorAfterSubmit,
});
Hook Parameters
rules (array) See rules API
Array of validation rules to be run on current form.
initialFormData (object)
Initial form data to set default values of current form.
hideBeforeSubmit (boolean) See Example
Flag to hide validation errors before setFormIsSubmitted function is called.
showAfterBlur (boolean) See Example
Flag to hide validation errors before setPathIsBlurred function is called for any path.
focusToErrorAfterSubmit (boolean) See Example
Flag to automatically focus to first HTML element with validation error after setFormIsSubmitted function is called.
elementFocusHandler (function) See Example
Function that accepts the first "elementId" of current validation errors as parameter that is used to customly manage DOM after submit.
Return Values
isValid (boolean)
Always returns the form's combined validation result according to current formData and rules.
validationError (object) See Example
Returns the validation result info of each rule with each path according to current formData and rules. Some validation results can be hide before submit or before blur.
validationErrorOriginalResult (object) See Example
Returns the validation result info of each rule with each path according to current formData and rules. (is not hidden before submit or before blur).
formData (object) See Example
Returns current formData.
formIsSubmitted (boolean) See Example
Returns current formIsSubmitted info.
blurredPathList (array) See Example
Returns current blurredPathList info.
setPathValue(path, value) See Path API - See Example
Function to update given path of formData with given value.
unsetPathValue(path, value) See Path API - See Example
Function to remove value and key for given path of formData.
setFormIsSubmitted() See Example
Function to set form is submitted info true and show all current validation results if hideBeforeSubmit parameter is set true.
setPathIsBlurred(path) See Path API
Function to set element of path is blurred info true and unhide its current validation result if showAfterBlur parameter is set true.
unsetPathIsBlurred(path) See Path API - See Example
Function to unset element of path is blurred info and hide its current validation result if showAfterBlur parameter is set true.
setFormData(newFormData, pathToBeRun) See Example
Function to update whole formData and run validation rules of given pathToBeRun array.
setRules(newRules) See Example
Function to update rules and run all validations according to these rules.
setFormDataAndRules(newFormData, newRules) See Example
Function to update whole formData and rules simultaneously and run all validations according to these formData and rules.
resetForm() See Example
Function to reset form submitted info and elements blurred info.
isPathValid(path) See Example
Function to get if path can be interpreted as valid.
getValue(path)
Function to get value of given path on formData object.
getError(path)
Function to get validation error of given path on validationError object.
forceRunAllValidations()
Function to run all validations according to current rules.