I have the following useState declared to hold the state data for my registration form. In there I have state variables for form fields and corresponding error messages for those form fields.
const [registerForm, setRegisterForm] = useState({
email: '',
emailError: '',
fullName: '',
fullNameError: '',
password: '',
passwordError: '',
confirmPassword: '',
confirmPasswordError: '',
country: '',
countryError: '',
state: '',
stateError: '',
suburb: '',
suburbError: ''
});
Below piece of code check for the validation issues in my register form (at the moment it has only email, full name & password validation). If there is a validation message, it will set the relevant state value.
// validate form inputs
const formInputValidation = () => {
let result = true;
// clear validation errors before setting any new validation messages
clearValidationMessages();
let emailResult = Validation.validateEmailInput(registerForm.email);
setValidationMessages('emailError', emailResult.message);
result = result && emailResult.isSuccess;
let fullNameResult = Validation.validateTextInput(Constants.AUTHENTICATION_FORMS.registerForm.fullName, registerForm.fullName);
setValidationMessages('fullNameError', fullNameResult.message);
result = result && fullNameResult.isSuccess;
let passwordResult = Validation.validateTextInput("password", registerForm.password);
setValidationMessages('passwordError', passwordResult.message);
result = result && passwordResult.isSuccess;
return result;
}
The following piece of code set the relevant error state variable based on the validation message (value) it received. As an example if the email is empty, it will set the validation message for emailError in the registerForm state.
// set validation messages
const setValidationMessages = (name, value) => {
switch (name) {
case 'emailError':
setRegisterForm({ ...registerForm, emailError: value });
break;
case 'fullNameError':
setRegisterForm({ ...registerForm, fullNameError: value });
break;
case 'passwordError':
setRegisterForm({ ...registerForm, passwordError: value });
break;
case 'confirmPasswordError':
setRegisterForm({ ...registerForm, confirmPasswordError: value });
break;
case 'countryError':
setRegisterForm({ ...registerForm, countryError: value });
break;
case 'stateError':
setRegisterForm({ ...registerForm, stateError: value });
break;
case 'suburbError':
setRegisterForm({ ...registerForm, suburbError: value });
break;
default:
break;
}
}
However when I click the register button without filling any of the fields, the error message is shown for the confirm password only. The email validation and full name validation is not shown.
What I expected was to see all the validation messages(email, full name & password).
My thinking is that, at the time of setting the validation message for password, the …registerForm is completely empty, in the sense the other validation messages (email & full name) are not set yet. Hence seeing only the password validation message.
I can declare each state variable for each form field and relevant error message and resolved this issue. But would like to know if it possible to achieve the same with my above implementation.
