diff --git a/src/Form/Formbot.js b/src/Form/Formbot.js index 92523fe..18b5e24 100644 --- a/src/Form/Formbot.js +++ b/src/Form/Formbot.js @@ -147,14 +147,22 @@ export default class Formbot extends React.Component { const fieldValue = this.state.values[field]; let errorMsg; - try { - if (hasSchema && typeof validation.validate === 'function') { - validation.validate(fieldValue, validationOpts).catch(e => { - this.setErrors({ [field]: e.message }, resolve); - }); + if (hasSchema && typeof validation.validate === 'function') { + validation.validate(fieldValue, validationOpts) + .catch(e => { + errorMsg = e.message; + }) + .finally(() => { + this.updateField(field, { validated: true }).then(() => { + this.setErrors({ [field]: errorMsg }, resolve); + }); + }) - return; - } else if (typeof validation === 'function') { + return; + } + + try { + if (typeof validation === 'function') { validation(fieldValue); } else { Object.keys(validation).forEach(method => { @@ -200,10 +208,9 @@ export default class Formbot extends React.Component { }, }, () => { - this.props.onChange(field, value, this.state.values); - this.updateField(field, { validated: false }).then(() => { - this.validateField(field); - }); + this.updateField(field, { validated: false }) + .then(() => this.validateField(field)) + .then(() => this.props.onChange(field, value, this.state.values)) } ); }; diff --git a/src/Form/Formbot.mdx b/src/Form/Formbot.mdx index 95510f0..79aceff 100644 --- a/src/Form/Formbot.mdx +++ b/src/Form/Formbot.mdx @@ -47,36 +47,29 @@ Quickly build a form using a Formbot and pre-configured form components. Formbot ### Async Validation -```jsx - + { + setTimeout(() => { + resolve(false); + }, 2000); + }); }), - }}> -
- - - - - - -
-
-``` + }}> +
+ + + + + +
+ +
+ ### Entire Form