Commit cec3214d authored by Matthew Taylor's avatar Matthew Taylor

some updates from feedback

thanks @rschamp!

1. moved org type validation to a custom validation
2. added a callback to the username validation call
parent f0c2280f
...@@ -12,6 +12,9 @@ module.exports.validations = { ...@@ -12,6 +12,9 @@ module.exports.validations = {
notEqualsField: function (values, value, field) { notEqualsField: function (values, value, field) {
return value !== values[field]; return value !== values[field];
}, },
checkboxRequired: function (values, value) {
return value.length > 0;
},
isPhone: function (values, value) { isPhone: function (values, value) {
if (typeof value === 'undefined') return true; if (typeof value === 'undefined') return true;
if (value && value.national_number === '+') return true; if (value && value.national_number === '+') return true;
......
...@@ -82,39 +82,50 @@ module.exports = { ...@@ -82,39 +82,50 @@ module.exports = {
onChangeShowPassword: function (field, value) { onChangeShowPassword: function (field, value) {
this.setState({showPassword: value}); this.setState({showPassword: value});
}, },
validateUsername: function (username) { validateUsername: function (username, callback) {
api({ api({
host: '', host: '',
uri: '/accounts/check_username/' + username + '/' uri: '/accounts/check_username/' + username + '/'
}, function (err, res) { }, function (err, body, res) {
var formatMessage = this.props.intl.formatMessage; var formatMessage = this.props.intl.formatMessage;
if (err) { if (err || res.statusCode !== 200) {
err = err || formatMessage({id: 'general.error'});
this.refs.form.refs.formsy.updateInputsWithError({all: err}); this.refs.form.refs.formsy.updateInputsWithError({all: err});
return false; if (typeof callback !== 'undefined') {
callback(false);
}
return;
} }
res = res[0]; body = body[0];
switch (res.msg) { var isValid = false;
switch (body.msg) {
case 'valid username': case 'valid username':
this.setState({ this.setState({
validUsername: 'pass' validUsername: 'pass'
}); });
return true; isValid = true;
break;
case 'username exists': case 'username exists':
this.refs.form.refs.formsy.updateInputsWithError({ this.refs.form.refs.formsy.updateInputsWithError({
'user.username': formatMessage({id: 'registration.validationUsernameExists'}) 'user.username': formatMessage({id: 'registration.validationUsernameExists'})
}); });
return false; break;
case 'bad username': case 'bad username':
this.refs.form.refs.formsy.updateInputsWithError({ this.refs.form.refs.formsy.updateInputsWithError({
'user.username': formatMessage({id: 'registration.validationUsernameVulgar'}) 'user.username': formatMessage({id: 'registration.validationUsernameVulgar'})
}); });
return false; break;
case 'invalid username': case 'invalid username':
default: default:
this.refs.form.refs.formsy.updateInputsWithError({ this.refs.form.refs.formsy.updateInputsWithError({
'user.username': formatMessage({id: 'registration.validationUsernameInvalid'}) 'user.username': formatMessage({id: 'registration.validationUsernameInvalid'})
}); });
return false; break;
}
if (typeof callback !== 'undefined') {
callback(isValid);
} }
}.bind(this)); }.bind(this));
}, },
...@@ -123,11 +134,10 @@ module.exports = { ...@@ -123,11 +134,10 @@ module.exports = {
}, },
onValidSubmit: function (formData) { onValidSubmit: function (formData) {
this.setState({waiting: true}); this.setState({waiting: true});
this.validateUsername(formData.user.username); this.validateUsername(formData.user.username, function (isValid) {
this.setState({waiting: false}); this.setState({waiting: false});
if (this.state.validUsername === 'pass') { if (isValid) return this.props.onNextStep(formData);
this.props.onNextStep(formData); }.bind(this));
}
}, },
render: function () { render: function () {
var formatMessage = this.props.intl.formatMessage; var formatMessage = this.props.intl.formatMessage;
...@@ -489,25 +499,7 @@ module.exports = { ...@@ -489,25 +499,7 @@ module.exports = {
onChooseOrganization: function (name, values) { onChooseOrganization: function (name, values) {
this.setState({otherDisabled: values.indexOf(this.organizationL10nStems.indexOf('orgChoiceOther')) === -1}); this.setState({otherDisabled: values.indexOf(this.organizationL10nStems.indexOf('orgChoiceOther')) === -1});
}, },
validateOrganizationType: function (formData) { onValidSubmit: function (formData) {
if (formData.organization.type.length < 1) {
return false;
}
return true;
},
onSubmit: function (formData, reset, invalidate) {
if (!this.validateOrganizationType(formData)) {
return invalidate({
'organization.type': this.props.intl.formatMessage({id: 'teacherRegistration.validationRequired'})
});
}
},
onValidSubmit: function (formData, reset, invalidate) {
if (!this.validateOrganizationType(formData)) {
return invalidate({
'organization.type': this.props.intl.formatMessage({id: 'teacherRegistration.validationRequired'})
});
}
return this.props.onNextStep(formData); return this.props.onNextStep(formData);
}, },
render: function () { render: function () {
...@@ -541,6 +533,14 @@ module.exports = { ...@@ -541,6 +533,14 @@ module.exports = {
value={[]} value={[]}
options={this.getOrganizationOptions()} options={this.getOrganizationOptions()}
onChange={this.onChooseOrganization} onChange={this.onChooseOrganization}
validations={{
checkboxRequired: true
}}
validationErrors={{
checkboxRequired: formatMessage({
id: 'teacherRegistration.validationRequired'
})
}}
required /> required />
</div> </div>
<div className="other-input"> <div className="other-input">
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
"general.discuss": "Discuss", "general.discuss": "Discuss",
"general.dmca": "DMCA", "general.dmca": "DMCA",
"general.emailAddress": "Email Address", "general.emailAddress": "Email Address",
"general.error": "Oops! Something went wrong",
"general.explore": "Explore", "general.explore": "Explore",
"general.faq": "FAQ", "general.faq": "FAQ",
"general.female": "Female", "general.female": "Female",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment