Created
October 22, 2015 12:49
-
-
Save Andrew8xx8/ada8caeebc7e4b3a9e59 to your computer and use it in GitHub Desktop.
React validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import AppDispatcher from '../dispatcher/AppDispatcher'; | |
import AuthConstants from '../constants/AuthConstants'; | |
import AuthRepository from '../repositories/AuthRepository'; | |
import SignUpValidator from '../validators/SignUpValidator'; | |
var actions = { | |
signUp (options) { | |
AppDispatcher.dispatch({ actionType: AuthConstants.AUTH_LOADING }); | |
return SignUpValidator.validate(options) | |
.then(() => { | |
return AuthRepository.signUp(options); | |
}) | |
.catch(error => { | |
AppDispatcher.dispatch({ actionType: AuthConstants.AUTH_FAIL, error }); | |
throw error; | |
}); | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
export default class FormValidationError { | |
constructor(message) { | |
this.name = "ValidationError"; | |
this.form = `backend_messages.${_.snakeCase(message)}`; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import _ from 'lodash'; | |
import ValidatorHelpers from '../lib/ValidatorHelpers'; | |
export default { | |
validate(fields) { | |
let errors = {}; | |
_.each(['email', 'username', 'password', 'passwordConfirm'], (field) => { | |
if (_.isEmpty(fields[field])) { | |
errors[field] = `error.${field}.empty`; | |
} | |
}); | |
if (!fields.acceptTerms) { | |
errors.acceptTerms = 'error.acceptTerms.empty'; | |
} | |
if (_.isString(fields.email) && fields.email.indexOf('@') < 0) { | |
errors.email = 'error.email.is_not_email'; | |
} | |
if (fields.password !== fields.passwordConfirm) { | |
errors.passwordConfirm = 'error.passwordConfirm.must_equal_password'; | |
} | |
return ValidatorHelpers.deferredError(errors); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment