# @lysyi3m/stimulus-use-validation

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

Latest version **1.3.0** (published 2021-08-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install @lysyi3m/stimulus-use-validation
pnpm add @lysyi3m/stimulus-use-validation
yarn add @lysyi3m/stimulus-use-validation
bun add @lysyi3m/stimulus-use-validation
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2021-08-08 |
| First published | 2020-12-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10.0.0 |
| Dependencies | 0 |
| Unpacked size | 14.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Emil Kashkevich |
| Maintainers | lysyi3m |

## Links

- npm: https://www.npmjs.com/package/@lysyi3m/stimulus-use-validation
- Repository: https://github.com/lysyi3m/stimulus-use-validation
- Homepage: https://github.com/lysyi3m/stimulus-use-validation#readme
- Issues: https://github.com/lysyi3m/stimulus-use-validation/issues
- npm.io page: https://npm.io/package/@lysyi3m/stimulus-use-validation

## Recent versions

- 1.3.0 (latest) — 2021-08-08
- 1.2.0 — 2021-03-16
- 1.1.1 — 2021-02-02
- 1.1.0 — 2021-02-02
- 1.0.3 — 2021-01-24
- 1.0.2 — 2021-01-24
- 1.0.1 — 2021-01-24
- 1.0.0 — 2020-12-13

## README

# stimulus-use-validation

![Node.js CI](https://github.com/lysyi3m/stimulus-use-validation/workflows/Node.js%20CI/badge.svg?branch=master)
[![npm version](https://badge.fury.io/js/%40lysyi3m%2Fstimulus-use-validation.svg)](https://badge.fury.io/js/%40lysyi3m%2Fstimulus-use-validation)

Lightweight, zero dependency, customizable mixin for [Stimulus](https://stimulusjs.org/) to handle form validation easily. Heavily rely on Constraint Validation API with support for custom validators.

## Installation

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

or using [Yarn](https://yarnpkg.com/):
```sh
$ yarn add @lysyi3m/stimulus-use-validation
```

## Usage

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

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

## Options

| Option| Description | Default value |
|-----------------------|-------------|---------------------|
| `disable` | Disable submit button if there are any errors | `true` |
| `errorClassName` | Class name attached to invalid form control | `""` |
| `errorSelector` | Selector to match DOM element, where validation message will be rendered  | `.help-block` |
| `form` | HTMLElement which form validation will be attached to. `this.element` if not specified | `undefined` |
| `parentErrorClassName` | Class name attached to DOM element, containing invalid form control | `has-error` |
| `parentSelector` | Selector to match DOM element, contain form control and error container elements | `.form-group` |
| `validators` | Custom 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:

```js

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

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

  return { isValid, message }
}

```

---
_Source: https://npm.io/package/@lysyi3m/stimulus-use-validation · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
