lcFormValidation
lcFormValidation copied to clipboard
Improve get formValidationResult.fieldErrors
When we are going to validate a form and we want to get fieldErrors, i.e in React, we have to :
(formValidationResult) => {
const dataFormErrors: DataFormErrors = { ...this.state.dataFormErrors };
formValidationResult.fieldErrors.forEach((fieldValidationResult) => {
dataFormErrors[fieldValidationResult.key] = fieldValidationResult;
});
this.setState({
...this.state,
dataFormErrors,
});
}
We could create a method helper to encapsulate this mapping, something like :
Pseudocode:
const mapFieldValidationResults = (array: FieldValidationResult[]) => {
const objectWithFields = {
[array[0].key] = array[0],
[array[1].key] = array[1],
.....
}
return objectWithFields;
}
On future lc-form-validation versions we could return this "objectWithFields"
directly on formValidationResult.fieldErrors
The scenario to cover:
-
User has a form with some validations, these validations errors are not shown because we don't fire them til the user interacts with the inputs (touched).
-
If the user click on the submit option, the validation form will be fired, but the error messages per field won't be updated.
We need an easy way to fullfill them.