react-validation
react-validation copied to clipboard
Returns a boolean after validation
- Added a
hasErrors()
method to return a boolean value -
validate()
andvalidateAll()
will return a boolean value after validation.
This PR enables support for the following use case.
<button
type="button"
onClick={event => {
const ok = this.form.validateAll();
if (!ok) {
return;
}
const { name, password } = this.form.getValues();
// Save changes
}}
>
Save
</button>
@cheton , did you tested it? I believe on that loop errors setup isn't made yet since _setErrors
called asynchronously and hasErrors
will get prevState
.
Sorry for that I did make a mistake, the _setErrors
callback is asynchronous and it will be called when the component state has actually updated.
How about using an error-first callback for the validate function?
this.form.validateAll((err) => {
if (err) {
return;
}
const { name, password } = this.form.getValues();
// Save changes
});
I made a little change to the code using the error-first callback, this should fix the issue existed in this PR.
I'd love this feature to validate the whole form before submitting. I have to do I manually with refs to all inputs inside a form and it's awful.