Last active
October 30, 2016 16:03
-
-
Save mkozhukharenko/149a395532144cfcc8db9bf38130179d to your computer and use it in GitHub Desktop.
onFieldChange implementation with validation
This file contains 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, observable} from 'mobx'; | |
import Validator from 'validatorjs'; | |
class LoginStore { | |
@observable | |
form = { | |
fields: { | |
email: { | |
value: '', | |
error: null, | |
rule: 'required|email' | |
}, | |
password: { | |
value: '', | |
error: null, | |
rule: 'required' | |
}, | |
}, | |
meta: { | |
isValid: true, | |
error: null, | |
}, | |
} | |
@action | |
onFieldChange = (field, value) => { | |
this.form.fields[field].value = value; | |
let {email, password} = this.form.fields | |
var validation = new Validator( | |
{email: email.value, password: password.value}, | |
{email: email.rule, password: password.rule}, | |
) | |
this.form.meta.isValid = validation.passes(); | |
this.form.fields[field].error = validation.errors.first(field) | |
}; | |
} | |
export default LoginStore; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment