You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

vanilla-form-manager

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vanilla-form-manager

A Package to handle your HTML Form in vanilla JS

0.0.8
latest
npmnpm
Version published
Maintainers
2
Created
Source

Vanilla Form Manager

A Small Package to handle your HTML Form in vanilla JS

Table of Contents
  • About The Project
  • Getting Started
  • Usage
  • Roadmap
  • Contributing
  • License
  • Contact
  • Acknowledgments

Getting Started

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

Installation

  • npm

    npm install vanilla-form-manager
    
  • yarn

    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

```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

```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

Examples

You can come to

FormValidation Config

  • receive T as generic type FormValidation, if T is not defined, T will automatically infer as initialValues type
APITypeDetail
formIdStringform id of the form
initialValuesObjectdefault values of the form
validationsObject of func(value) => stringdefined validation of each field
validateOnChangeBooleanenable validation on field change
validateOnBlurBooleanenable validation on field blur
onSubmitfunc(values: T)callback executed on form submitted
onChangefunc(values: T)callback executed on form change
onBlurfunc(values: T)callback executed on form blur
renderErrorfunc(formState: FormState,formEl: HTMLFormElement,formInputs: HTMLInputElement[])custom render error
watchObject of func(value, error, isTouched)config watch callback of each field change
debugBooleanenable debug console

FormValidation instance

APITypeDetail
initialValuesTinitial value get from config
isFormValidBooleanreturn true if the form has no error
formStateObjectcontain all information of the form (included: values,touched,errors)
formElementHTMLFormElementreturn form Element
resetFormfunc()reset form value to initial value
validateFieldfunc(fieldPath)manual valid a field
validateFormfunc()manual validate the form
renderFormValuefunc(value)force form to render form again with new value
formValuesObjectreturn form values
setFormValuefunc(value)set form value
removeFormValuefunc(fieldPath)remove a field value
getFieldValuefunc(fieldPath)get field value
setFieldValuefunc({fieldPath, value})set field value
setFieldTouchedfunc({fieldPath, touched})set field touch
getFieldTouchedfunc(fieldPath)get field touch
isFieldTouched(fieldPath) => booleanreturn is a field was touched or not
formErrorsObjectreturn form errors
removeFieldErrorfunc(fieldPath)remove error of a fieldPath
getFieldError(fieldPath) => stringreturn field's error
setFieldErrorfunc({fieldPath, error})manual set a field error
addArrayItemfunc({fieldPath, value}, callback)executed callback and add a new field into an array
removeArrayItemfunc({field, index}, callback)remove field at specific index and executed callback func

Keywords

form manager

FAQs

Package last updated on 10 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts