Skip to content

Instantly share code, notes, and snippets.

@kettanaito
Last active April 3, 2018 13:18
Show Gist options
  • Save kettanaito/0408b5e6bb13b3a748a108cfee8af93e to your computer and use it in GitHub Desktop.
Save kettanaito/0408b5e6bb13b3a748a108cfee8af93e to your computer and use it in GitHub Desktop.
React Advanced Form - Async validation
import React from 'react';
import { Form } from 'react-advanced-form';
export default class RegistrationForm extends React.Component {
validateEmail = ({ value, fieldProps, fields, form }) => {
return fetch('https://backend/', { body: value })
.then(res => res.json())
.then((res) => {
return {
/* Determine if the e-mail is valid based on response */
valid: (res.statusCode === 'SUCCESS'),
errorCode: res.errorCode
};
});
}
render() {
return (
<Form>
<Input
name="userEmail"
type="email"
asyncRule={this.validateEmail}
required />
{ /* Rest fields */ }
</Form>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment