# vanilla-form-manager

> A Package to handle your HTML Form in vanilla JS

Latest version **0.0.8** (published 2024-02-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install vanilla-form-manager
pnpm add vanilla-form-manager
yarn add vanilla-form-manager
bun add vanilla-form-manager
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2024-02-10 |
| First published | 2023-07-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 35.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Phu Nguyen, Duc Dau |
| Maintainers | dauleduc2, phunguyen713 |
| Keywords | form manager, vanilla form, html, form, state, management, validation, error, render, validation, form validation, form manager, form state, form error, form handling, HTML form |

## Links

- npm: https://www.npmjs.com/package/vanilla-form-manager
- npm.io page: https://npm.io/package/vanilla-form-manager

## Dependencies (2)

- [vanilla-form-manager](https://npm.io/package/vanilla-form-manager.md) ^0.0.7
- [terser-webpack-plugin](https://npm.io/package/terser-webpack-plugin.md) ^5.3.9

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.8 (latest) — 2024-02-10
- 0.0.7 — 2023-07-24
- 0.0.6 — 2023-07-20
- 0.0.5 — 2023-07-20
- 0.0.4 — 2023-07-20
- 0.0.3 — 2023-07-20
- 0.0.2 — 2023-07-20
- 0.0.1 — 2023-07-20

## README

<div align="center">
  <h3 align="center">Vanilla Form Manager</h3>

  <p align="center">
   A Small Package to handle your HTML Form in vanilla JS
  </p>
</div>

<!-- TABLE OF CONTENTS -->
<details>
  <summary>Table of Contents</summary>
  <ol>
    <li>
      <a href="#about-the-project">About The Project</a>
      <ul>
        <li><a href="#built-with">Built With</a></li>
      </ul>
    </li>
    <li>
      <a href="#getting-started">Getting Started</a>
      <ul>
        <li><a href="#prerequisites">Prerequisites</a></li>
        <li><a href="#installation">Installation</a></li>
      </ul>
    </li>
    <li><a href="#usage">Usage</a></li>
    <li><a href="#roadmap">Roadmap</a></li>
    <li><a href="#contributing">Contributing</a></li>
    <li><a href="#license">License</a></li>
    <li><a href="#contact">Contact</a></li>
    <li><a href="#acknowledgments">Acknowledgments</a></li>
  </ol>
</details>

## Getting Started

This is an example of how you may give instructions on setting up how to use the packages.

### Installation

- npm

  ```sh
  npm install vanilla-form-manager
  ```

- yarn
  ```sh
  yarn add vanilla-form-manager
  ```

## Note

The project is support TypeScript, so we recommended to used TypeScript to reduce mistake from typing.

# Usage

## Create a form instance

Create a simple instance of form

````md
```js
const initialValues = {
  fname: "Duc",
  age: "",
  address: {
    city: "",
    country: "",
  },
  scores: [{ math: "" }, { physics: "" }],
  hobbies: [""],
};

const myForm = new FormValidation({
  formId: "myForm",
  initialValues,
  validations: {
    fname: (value) => (!!value ? "" : "required"),
    age: (value) => (!!value ? "" : "required"),
    hobbies: (value) => (value.length > 1 ? "" : "at least 2 hobbies"),
    "hobbies._item": (value) => (!!value ? "" : "Required"),
  },
  onSubmit: (values) => {
    console.log("form submitted", values);
  },
});
```
````

- `formId` : is form ID that can be use later to interact with the form
- `initialValues`: initial values of the form
- `validations`: validation of the form, at currently we only support for single-field validate only (multiple fields with other packages will be in feature)
- `onSubmit`: callback on the form be submitted

## Debug the form

We currently support for debug mode to easier to track the form change. You can use it by adding `debug: true` into form config

````md
```js
const myForm = new FormValidation({
  // others config,
  debug: true,
});
```
````

- After set debug to `true`, you can use the toggle button at right bottom of screen to open the debug console

![Home Page](document/debug_console.png)

## Examples

You can come to

## FormValidation Config

- receive T as generic type FormValidation<T>, if T is not defined, T will automatically infer as initialValues type

| API              | Type                                                                                 | Detail                                     |
| ---------------- | ------------------------------------------------------------------------------------ | ------------------------------------------ |
| formId           | String                                                                               | form id of the form                        |
| initialValues    | Object                                                                               | default values of the form                 |
| validations      | Object of func(value) => string                                                      | defined validation of each field           |
| validateOnChange | Boolean                                                                              | enable validation on field change          |
| validateOnBlur   | Boolean                                                                              | enable validation on field blur            |
| onSubmit         | func(values: T)                                                                      | callback executed on form submitted        |
| onChange         | func(values: T)                                                                      | callback executed on form change           |
| onBlur           | func(values: T)                                                                      | callback executed on form blur             |
| renderError      | func(formState: FormState<T>,formEl: HTMLFormElement,formInputs: HTMLInputElement[]) | custom render error                        |
| watch            | Object of func(value, error, isTouched)                                              | config watch callback of each field change |
| debug            | Boolean                                                                              | enable debug console                       |

## FormValidation instance

| API              | Type                               | Detail                                                                      |
| ---------------- | ---------------------------------- | --------------------------------------------------------------------------- |
| initialValues    | T                                  | initial value get from config                                               |
| isFormValid      | Boolean                            | return `true` if the form has no error                                      |
| formState        | Object                             | contain all information of the form (included: `values`,`touched`,`errors`) |
| formElement      | HTMLFormElement                    | return form Element                                                         |
| resetForm        | func()                             | reset form value to initial value                                           |
| validateField    | func(fieldPath)                    | manual valid a field                                                        |
| validateForm     | func()                             | manual validate the form                                                    |
| renderFormValue  | func(value)                        | force form to render form again with new value                              |
| formValues       | Object                             | return form values                                                          |
| setFormValue     | func(value)                        | set form value                                                              |
| removeFormValue  | func(fieldPath)                    | remove a field value                                                        |
| getFieldValue    | func(fieldPath)                    | get field value                                                             |
| setFieldValue    | func({fieldPath, value})           | set field value                                                             |
| setFieldTouched  | func({fieldPath, touched})         | set field touch                                                             |
| getFieldTouched  | func(fieldPath)                    | get field touch                                                             |
| isFieldTouched   | (fieldPath) => boolean             | return is a field was touched or not                                        |
| formErrors       | Object                             | return form errors                                                          |
| removeFieldError | func(fieldPath)                    | remove error of a fieldPath                                                 |
| getFieldError    | (fieldPath) => string              | return field's error                                                        |
| setFieldError    | func({fieldPath, error})           | manual set a field error                                                    |
| addArrayItem     | func({fieldPath, value}, callback) | executed callback and add a new field into an array                         |
| removeArrayItem  | func({field, index}, callback)     | remove field at specific index and executed callback func                   |

---
_Source: https://npm.io/package/vanilla-form-manager · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
