A framework agnostic, customizable form validator
npm install another-form-validator
import FormValidator from 'another-form-validator'
let model = {
pineapple: 1
}
const formValidator = new FormValidator()
formValidator.registerField({
name: 'pineapple',
model: () => model.pineapple,
validators: [{
getError: () => 'multiple pineapples are required',
validate: (value) => value > 1
}],
onChange: [
(field) => {
// called on field events (touch, dirty, validate)
if (field.errors.length) {
const errorToDisplay = field.errors[0]
}
}
]
})
model.pineapple = 2
formValidator.touch('pineapple')
formValidator.dirty('pineapple')
const formIsValid = formValidator.validate()
console.log(formIsValid) // true