1.2.2 • Published 5 years ago

@djadrianof/vform v1.2.2

Weekly downloads
13
License
MIT
Repository
github
Last release
5 years ago

alt text

VForm

A dependency-free vanilla Javascript form validation using Constraint Validation API. Just use HTML5 form fields based in validation attributes and that's it, the library resolve all!

See more about HTML validation attributes in: MDN - Constraint validation

Install

NPM:

npm i @djadrianof/vform --saveDev

Yarn:

yarn add @djadrianof/vform -D

Setup

Import the library in your code:

import VForm from "@djadrianof/vform";

Or get the script directly from unpkg.com

<script src="https://unpkg.com/@djadrianof/vform"></script>

Usage

First initialize the library:

document.addEventListener("DOMContentLoaded", event => {
  const Validation = new VForm(".form", {
    classes: {
      errorElement: "field-error"
    },
    events: {
      onInitializedSuccess: onInitializedSuccessForm,
      onInitializedError: onInitializedError,
      onValid: onValid,
      onBlurFieldChecked: onBlurFieldChecked,
      onChangeFieldChecked: onChangeFieldChecked,
      onSubmit: onSubmit
    }
  });
});

Live Examples

Set your form element

Obs: Set the attribute noValidate in your form to prevent default browser validation.

  <form class="form" noValidate>
    <label data-field-container="name">
      <input
        type="email"
        name="name"
        required
        class="foo"
        minlength="4"
        data-empty-message="Empty message"
        data-error-message="Error message"
        data-length-message="Length message"
      />
      <span class="field-error"></span>
    </label>
  </form>

Options

Is possible set some options like css classes and events:

Classes

Classes are helpful for indicate the state of field or form.

const Validation = new VForm(".form", {
  classes: {
    valid: "field-valid",
    invalid: "field-invalid",
    formValid: "form-valid",
    errorElement: "error-element"
  }
});
NameDescription
validclass that will be set in field after be validated
invalidclass that will be set in field after check the status and return invalid
formValidclass that will be set in form when is valid
errorElementclass of element that is render the error message

Events

Events are helpful to get the status of form and fields.

const Validation = new VForm(".form", {
  events: {
    onInitializedSuccess: onInitializedSuccessForm,
    onInitializedError: onInitializedError,
    onSubmit: onSubmit,
    onReset: onReset,
    onValid: onValid,
    onFocus: onFocus,
    onBlurFieldChecked: onBlurFieldChecked,
    onChangeFieldChecked: onChangeFieldChecked,
    execBeforeSubmit: execBeforeSubmit
  }
});
NameDescription
onInitializedSuccessEvent is called if library is initialized with success. This method return the form DOM element
onInitializedErrorEvent is called if library is not initialized
onSubmitEvent is called when the form is submited. If form is valid, the method return a promise with a object that contains the fields
onResetEvent is called when the form is reseted
onValidEvent is called when the form stay valid
onFocusEvent is called when the field is invalid. The method return the field element
onBlurFieldCheckedEvent is called when the blur is executed and the field is valid. The method return the field element
onChangeFieldCheckedEvent is called after the field element stay valid and in onChange listener. The method return the field element
execBeforeSubmitIs executed before the submit of form. This method should return a promise

Data Attributes

Data attributes helps you to control and customize the validation.

<label data-field-container="name">
    <input
        type="email"
        name="name"
        required
        class="foo"
        minlength="4"
        data-empty-message="Empty message"
        data-error-message="Error message"
        data-length-message="Length message"
    />
    <span class="field-error"></span>
</label>

With a reference error element

<label data-field-container="age">
    <input
        type="text"
        name="age"
        required
        class="foo"
        minlength="4"
        data-empty-message="Empty message"
        data-error-message="Error message"
        data-length-message="Length message"
    />
    <span class="field-error" data-reference-error="age"></span>
</label>
NameDescription
data-field-containerThis data indicate a container for field. The value should be the same value of name attribute of field element.
data-error-messageMessage for indicate that field contains error
data-empty-messageMessage for indicate that field is empty
data-length-messageMessage for indicate that field needs contain a minimum size of characteres based on minlength attribute
data-reference-errorThis data indicate a different error element to show a error (use the same name of element that you need reference)

Methods

Are some helpful methods that you can access directly via library instance.

NameDescription
updateIs helpful if needs insert a new field dynamically in form or needs remove some field of form. This method will register the listeners and set validations for new fields.
getFieldsValuesReturn a promise that contain a object with fields values
setValidFieldIndicate that a field is valid
setInValidFieldIndicate that a field is not valid
setErrorMessageSet a error message dynamically for a field

Browser Support

  • IE 11+
  • Chrome 49+
  • Safari 10+
  • IOS Safari 10+
  • Firefox 29+
1.2.2

5 years ago

1.2.1

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago