Created
October 30, 2016 17:11
-
-
Save mkozhukharenko/6f174a42c1c5fe1b30a8cfaa1998b75f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {action, toJS} from 'mobx' | |
import Validator from 'validatorjs'; | |
class FormStore { | |
getFlattenedValues = (valueKey = 'value') => { | |
let data = {}; | |
let form = toJS(this.form).fields; | |
Object.keys(form).map(key => { | |
data[key] = form[key][valueKey] | |
}); | |
return data | |
}; | |
@action | |
onFieldChange = (field, value) => { | |
this.form.fields[field].value = value; | |
var validation = new Validator( | |
this.getFlattenedValues('value'), | |
this.getFlattenedValues('rule')); | |
this.form.meta.isValid = validation.passes(); | |
this.form.fields[field].error = validation.errors.first(field) | |
}; | |
@action | |
setError = (errMsg) => { | |
this.form.meta.error = errMsg | |
} | |
} | |
export default FormStore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment