0.0.8 • Published 3 months ago

vanilla-form-manager v0.0.8

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

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