/another-form-validator

A framework agnostic, customizable form validator

Primary LanguageTypeScriptMIT LicenseMIT

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