1
I have a form that when I click send leaves red the field that is empty. It makes different validations in each of the fields. Only when all fields are gray border it sends the data via post and disables the error message.
The problem
Within the handleClick method it makes the status changes if the field is correctly filled and at the end it makes a final check on all fields. If everything is gray sends the data, if it does not maintain the error. But the first time I click sends even being wrong, and only the second time I click discovers the error. This happens because you are entering my if before making the changes in the fields using setState
. I tried to use setTimeout but it didn’t work and I was told it wasn’t good practice. How to proceed if is async and only be activated when States are up to date?
if ((valueColor && nameColor && lastNameColor && emailColor
&& cardColor && cvvColor && cpfColor
&& dateColor && periodicityColor) === 'grey') {
console.log('dentro'); this.errorMessage.message = 'none'; //this.handleSubmit();
} else {
console.log('fora'); this.errorMessage.message = 'block';
}
Acting on the basis of style rather than the outcome of validation is bad practice, and it becomes evident in your case. How are the fields being validated? Also put the code that validates the question
– Isac
That’s a good tip, kid, I’ll look into it.
– Danilo Silva