Skip to content

Instantly share code, notes, and snippets.

@neurodynamic
Last active July 17, 2018 12:55
Show Gist options
  • Save neurodynamic/25c6537ff28d5577f319 to your computer and use it in GitHub Desktop.
Save neurodynamic/25c6537ff28d5577f319 to your computer and use it in GitHub Desktop.
AJAX validation with redux-form example
/* EXPLANATIONS/EXAMPLES THAT MAY PROVIDE HELPFUL REFERENCE
http://erikras.github.io/redux-form/#/examples/asynchronous-blur-validation
https://github.com/erikras/react-redux-universal-hot-example/blob/b46921a990991e85522dbfc3195a46490a8f4e90/src/components/SurveyForm/SurveyForm.js
https://github.com/erikras/redux-form/issues/119
*/
// Working function with real request
export function asyncValidate (data) {
if (!data.email) {
return Promise.resolve({})
}
return new Promise((resolve, reject) => {
$.get(
window.location.origin +
'/email_is_unique',
{email: data.email}
).done(
function (response) {
if (response === false) {
reject({email: 'This email has already been taken'})
} else {
resolve()
}
}
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment