Skip to content

Instantly share code, notes, and snippets.

@mwangaben
Created February 9, 2019 04:15
Show Gist options
  • Save mwangaben/fe77ad7ed6459b487f47ba8a922e107d to your computer and use it in GitHub Desktop.
Save mwangaben/fe77ad7ed6459b487f47ba8a922e107d to your computer and use it in GitHub Desktop.
import _ from 'lodash';
class MyForm {
constructor(defaults = null) {
this.error = {};
this.defaults =defaults
if(this.defaults){
Object.assign(this, this.defaults);
}
}
hasError(field) {
return this.error.hasOwnProperty(field);
}
errorOut(field) {
let b;
_.forEach(this.error, (sms, key) => {
if (key === field) {
b = sms[0];
}
});
return b
}
clearAll() {
return this.error = {}
}
clear(field) {
delete this.error[field];
}
any() {
return _.isEmpty(this.error);
}
reset() {
let fields = Object.keys(this);
_.forEach(fields, (value) => {
this[value] = '';
});
return this;
}
}
export default MyForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment