basic version of ValidationMixin, register page getting more functional

This commit is contained in:
Edin Dazdarevic
2015-02-23 21:30:06 +01:00
parent 9c152e8adb
commit e1bba9d8db
4 changed files with 174 additions and 23 deletions

View File

@@ -0,0 +1,51 @@
var RibicaValidationMixin = {
_updateState: function(prop, value) {
var newState = {};
newState[prop] = value;
this.setState(newState);
},
_validate: function(prop, value) {
if(this.validations && this.validations[prop]) {
var cb = function(err) {
if (err !== undefined) {
var errors = this.state.errors;
if(err.length > 0) {
errors[prop] = err;
} else {
delete errors[prop];
}
this.setState({errors: errors});
}
}.bind(this);
var immediateErrors = this.validations[prop](value, cb);
if(immediateErrors !== undefined) {
cb(immediateErrors);
}
}
},
handleChange: function(prop) {
return function(event) {
event.preventDefault();
this._updateState(prop, event.target.value);
this._validate(prop, event.target.value);
}.bind(this);
},
getValidationMessages:function(prop) {
return (this.state.errors[prop] || []);
},
validate: function() {
for (var key in this.state) {
if (this.state.hasOwnProperty(key)) {
this._validate(key, this.state[key]);
}
}
return this.isValid();
},
isValid: function() {
return Object.keys(this.state.errors).length === 0;
}
};
module.exports = RibicaValidationMixin;