validation mixin small fixes
This commit is contained in:
@@ -12,7 +12,7 @@ post '/user' do
|
|||||||
user.from_json(json, false)
|
user.from_json(json, false)
|
||||||
|
|
||||||
if user.save
|
if user.save
|
||||||
"ok"
|
{:status => "ok"}.to_json
|
||||||
else
|
else
|
||||||
status 400
|
status 400
|
||||||
user.errors.to_json
|
user.errors.to_json
|
||||||
|
|||||||
@@ -91,17 +91,32 @@ var Register = React.createClass({
|
|||||||
},
|
},
|
||||||
validations: {
|
validations: {
|
||||||
firstName: function(value) {
|
firstName: function(value) {
|
||||||
var errors = [];
|
|
||||||
if (!value || value === '') {
|
if (!value || value === '') {
|
||||||
errors.push("Ime je obavezno.");
|
return ["Ime je obavezno."];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
password: function(value, callback) {
|
||||||
|
if(value === undefined || value === "") {
|
||||||
|
return ["Šifra je obavezna."];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.length < 6) {
|
||||||
|
return ["Šifra mora sadržavati minimalno 6 karaktera."]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
callback([], ["passwordConfirmation"]);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
passwordConfirmation: function(value) {
|
||||||
|
if (this.getState().password !== value) {
|
||||||
|
return ["Šifre se ne podudaraju."];
|
||||||
}
|
}
|
||||||
return errors;
|
|
||||||
},
|
},
|
||||||
lastName : function(value) {
|
lastName : function(value) {
|
||||||
if (value === 'Dazdarevic') {
|
if (!value || value === '') {
|
||||||
return ["You're the owner!"];
|
return ["Prezime je obavezno."];
|
||||||
}
|
}
|
||||||
return [];
|
|
||||||
},
|
},
|
||||||
_emailRe: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
_emailRe: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
||||||
_emailTimeoutId: null,
|
_emailTimeoutId: null,
|
||||||
@@ -125,6 +140,7 @@ var Register = React.createClass({
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
//}.bind(this), 1000);
|
//}.bind(this), 1000);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return ['Neispravna email adresa.'];
|
return ['Neispravna email adresa.'];
|
||||||
}
|
}
|
||||||
@@ -196,13 +212,17 @@ var Register = React.createClass({
|
|||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<label for="password" className="col-md-4 control-label">Šifra</label>
|
<label for="password" className="col-md-4 control-label">Šifra</label>
|
||||||
<div className="col-md-4">
|
<div className="col-md-4">
|
||||||
<input type="password" valueLink={this.linkState('password')} className="form-control" id="password" placeholder="Šifra"/>
|
<input type="password" onChange={this.handleChange('password')} className="form-control" id="password" placeholder="Šifra"/>
|
||||||
|
{this.getValidationMessages('password').map(this.renderErrorMessage)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<label for="password_confirmation" className="col-md-4 control-label">Potvrda šifre</label>
|
<label for="password_confirmation" className="col-md-4 control-label">Potvrda šifre</label>
|
||||||
<div className="col-md-4">
|
<div className="col-md-4">
|
||||||
<input type="password" valueLink={this.linkState('passwordConfirmation')} className="form-control" id="password_confirmation" placeholder="Podvrda šifre"/>
|
<input type="password" onChange={this.handleChange('passwordConfirmation')} className="form-control" id="password_confirmation" placeholder="Podvrda šifre"/>
|
||||||
|
|
||||||
|
|
||||||
|
{this.getValidationMessages('passwordConfirmation').map(this.renderErrorMessage)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -218,7 +238,10 @@ var Register = React.createClass({
|
|||||||
|
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<div className="col-md-4">
|
<div className="col-md-4">
|
||||||
<button onClick={this.doRegister}>Save</button>
|
|
||||||
|
<button onClick={this.doRegister} type="button" className="btn btn-default btn-lg">
|
||||||
|
Register2
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|||||||
@@ -1,40 +1,54 @@
|
|||||||
var RibicaValidationMixin = {
|
var RibicaValidationMixin = {
|
||||||
_updateState: function(prop, value) {
|
_updateState: function(prop, value, callback) {
|
||||||
var newState = {};
|
var newState = {};
|
||||||
newState[prop] = value;
|
newState[prop] = value;
|
||||||
this.setState(newState);
|
this.setState(newState, function(){
|
||||||
|
if(callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}.bind(this));
|
||||||
},
|
},
|
||||||
_validate: function(prop, value) {
|
_validate: function(prop, value) {
|
||||||
if(this.validations && this.validations[prop]) {
|
if(this.validations && this.validations[prop]) {
|
||||||
var cb = function(err) {
|
var cb = function(err, revalidateFields) {
|
||||||
|
var errors = this.state.errors;
|
||||||
if (err !== undefined) {
|
if (err !== undefined) {
|
||||||
var errors = this.state.errors;
|
|
||||||
if(err.length > 0) {
|
if(err.length > 0) {
|
||||||
errors[prop] = err;
|
errors[prop] = err;
|
||||||
} else {
|
} else {
|
||||||
delete errors[prop];
|
delete errors[prop];
|
||||||
}
|
}
|
||||||
this.setState({errors: errors});
|
} else {
|
||||||
|
delete errors[prop];
|
||||||
|
}
|
||||||
|
this.setState({errors: errors});
|
||||||
|
if (revalidateFields) {
|
||||||
|
for(var i = 0; i < revalidateFields.length; i++) {
|
||||||
|
this.validateField(revalidateFields[i], this.state[revalidateFields[i]]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
var immediateErrors = this.validations[prop](value, cb);
|
var immediateErrors = this.validations[prop](value, cb);
|
||||||
if(immediateErrors !== undefined) {
|
cb(immediateErrors);
|
||||||
cb(immediateErrors);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handleChange: function(prop) {
|
handleChange: function(prop) {
|
||||||
return function(event) {
|
return function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
this._updateState(prop, event.target.value);
|
var target = event.target;
|
||||||
this._validate(prop, event.target.value);
|
this._updateState(prop, target.value, function(){
|
||||||
|
this._validate(prop, target.value);
|
||||||
|
}.bind(this));
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
},
|
},
|
||||||
getValidationMessages:function(prop) {
|
getValidationMessages:function(prop) {
|
||||||
return (this.state.errors[prop] || []);
|
return (this.state.errors[prop] || []);
|
||||||
},
|
},
|
||||||
|
validateField: function(field) {
|
||||||
|
this._validate(field, this.state[field]);
|
||||||
|
},
|
||||||
validate: function() {
|
validate: function() {
|
||||||
for (var key in this.state) {
|
for (var key in this.state) {
|
||||||
if (this.state.hasOwnProperty(key)) {
|
if (this.state.hasOwnProperty(key)) {
|
||||||
@@ -45,7 +59,15 @@ var RibicaValidationMixin = {
|
|||||||
},
|
},
|
||||||
isValid: function() {
|
isValid: function() {
|
||||||
return Object.keys(this.state.errors).length === 0;
|
return Object.keys(this.state.errors).length === 0;
|
||||||
}
|
},
|
||||||
|
componentDidMount : function(){
|
||||||
|
if (this.validations) {
|
||||||
|
var self = this;
|
||||||
|
this.validations.getState = function() {
|
||||||
|
return self.state;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = RibicaValidationMixin;
|
module.exports = RibicaValidationMixin;
|
||||||
|
|||||||
Reference in New Issue
Block a user