0.1.5 • Published 5 years ago

another-form-validator v0.1.5

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Another Form Validator

A framework agnostic, customizable form validator

NPM Version

Install

npm install another-form-validator

Usage

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

License

MIT