Skip to content
Snippets Groups Projects
Commit ecfdd359 authored by David Baker's avatar David Baker
Browse files

Validate everything on form submit. Don't use pwd1 where we didn't define it &...

Validate everything on form submit. Don't use pwd1 where we didn't define it & fix some error codes.
parent 02e41450
No related branches found
No related tags found
No related merge requests found
......@@ -65,9 +65,20 @@ module.exports = React.createClass({
onSubmit: function(ev) {
ev.preventDefault();
// validate everything, in reverse order so
// the error that ends up being displayed
// is the one from the first invalid field.
// It's not super ideal that this just calls
// onError once for each invalid field.
this.validateField(FIELD_PASSWORD_CONFIRM);
this.validateField(FIELD_PASSWORD);
this.validateField(FIELD_USERNAME);
this.validateField(FIELD_EMAIL);
if (this.allFieldsValid()) {
var promise = this.props.onRegisterClick({
username: this.refs.username.value.trim(),
password: pwd1,
password: this.refs.password.value.trim(),
email: this.refs.email.value.trim()
});
......@@ -77,6 +88,21 @@ module.exports = React.createClass({
ev.target.disabled = false;
});
}
}
},
/**
* Returns true if all fields were valid last time
* they were validated.
*/
allFieldsValid: function() {
var keys = Object.keys(this.state.fieldValid);
for (var i = 0; i < keys.length; ++i) {
if (this.state.fieldValid[keys[i]] == false) {
return false;
}
}
return true;
},
validateField: function(field_id) {
......@@ -120,24 +146,17 @@ module.exports = React.createClass({
this.markFieldValid(
field_id,
false,
"RegistrationForm.ERR_PASSWORD_MISSING"
"RegistrationForm.ERR_PASSWORD_LENGTH"
);
} else {
this.markFieldValid(field_id, true);
}
break;
case FIELD_PASSWORD_CONFIRM:
if (pwd1 == '') {
this.markFieldValid(
field_id, false,
"RegistrationForm.ERR_PASSWORD_MISSING"
field_id, pwd1 == pwd2,
"RegistrationForm.ERR_PASSWORD_MISMATCH"
);
} else if (pwd1 != pwd2) {
this.markFieldValid(
field_id, false,
"RegistrationForm.ERR_PASSWORD_LENGTH"
);
} else {
this.markFieldValid(field_id, true);
}
break;
}
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment