1.3.0 • Published 3 years ago

@lysyi3m/stimulus-use-validation v1.3.0

Weekly downloads
32
License
ISC
Repository
github
Last release
3 years ago

stimulus-use-validation

Node.js CI npm version

Lightweight, zero dependency, customizable mixin for Stimulus to handle form validation easily. Heavily rely on Constraint Validation API with support for custom validators.

Installation

$ npm install --save @lysyi3m/stimulus-use-validation

or using Yarn:

$ yarn add @lysyi3m/stimulus-use-validation

Usage

import { Controller } from 'stimulus'
import useValidation from '@lysyi3m/stimulus-use-validation'

export default class extends Controller {
  connect() {
    useValidation(this, {
      // options
    })
  }
}

Options

OptionDescriptionDefault value
disableDisable submit button if there are any errorstrue
errorClassNameClass name attached to invalid form control""
errorSelectorSelector to match DOM element, where validation message will be rendered.help-block
formHTMLElement which form validation will be attached to. this.element if not specifiedundefined
parentErrorClassNameClass name attached to DOM element, containing invalid form controlhas-error
parentSelectorSelector to match DOM element, contain form control and error container elements.form-group
validatorsCustom validators, mapped like '[field_name]': validatorFunc{}

Validators

Custom validators are functions, which accept two arguments: field and form (useful for complex validations implementation, based on several form fields) and return an object like { isValid: Bool, message: String }

Example:

const customPasswordValidator = (field, form) => {
  const isValid = field.value !== 'password'

  const message = "Password should not be 'password'"

  return { isValid, message }
}
1.3.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago