npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

forms-and-fields

v1.1.2

Published

A library to validation fields and submission forms

Downloads

7

Readme

Forms and Fields

NPM Version GitHub Workflow Status

A library to validation fields and submission forms.

For validation you can use required validator, one of validator validators (e.g. isEmail) or custom validator (including AJAX).

Usage

HTML:

<form class="js-form">
    <div>
        <input type="text" name="email" class="js-email"/>
    </div>
    <button type="submit">Submit</button>
</form>

JS:

import Form from 'forms-and-fields';
import Field from 'forms-and-fields';
import SubmissionResult from 'forms-and-fields';

const form = new Form('.js-form', [
   new Field('.js-email', ['required', 'isEmail'], {
        invalidMessages: {
            required: 'Email is required',
            isEmail: 'Invalid email'
        }
   })
], {
    onAfterFieldValidate: (validationResult) => {
        console.log(validationResult);
    },
    submit: (formData) => {
        console.log(formData);
        // here can be AJAX-request
        return new SubmissionResult(true);
    }
});

form.listen();

Console:

When you submit form with empty email:

ValidationResult {result: false, message: "Email is required"}

When you submit form with invalid email:

ValidationResult {result: false, message: "Invalid email"}

When you submit form with valid email:

ValidationResult {result: true, message: ""}
FormData {...}

Extended example:

See more extended examples in sandbox

Installation

npm install forms-and-fields

Documentation

Common

For validation and submission form you need create Form object (with needed Field objects) and call listen method.

Form validation is all fields validation and form is valid when all fields are valid.

For validation separate fields you need create Field object and call listen method.

Custom validators must return ValidationResult (can return Promise, so you can use AJAX).

Form submit callback must return SubmissionResult (can return Promise, so you can use AJAX).

Validation is triggered when user submits form. Form is not submitting if form is invalid.

ValidationResult class

| Method | Description | |----------------------------------------------------------|--------------------------------------------| | constructor(result: boolean, message?: string = '') | Initializes validation result object | | isValid(): bool | Returns boolean validation result | | hasMessage(): bool | Returns validation result message existing | | setMessage(message: string): void | Sets validation result message | | getMessage(): string | Returns validation result message |

SubmissionResult class

| Method | Description | |-------------------------------------------------------|----------------------------------------------------| | constructor(result: boolean, data?: object = {}) | Initializes submission result object with any data | | isSuccess(): boolean | Returns boolean submission result | | getData(): object | Returns data |

Fields

Field class

| Method | Description | |---------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------| | constructor (fieldSelector: string, validators?: object, options?: object = {}) | Initializes field object with validators (see Field validators) and options (see Field options) for field with fieldSelector selector | | setOptions(options: object): void | Sets options (see Field options) | | listen(): void | Starts listening field for triggering validation and trimming (if enabled) | | isValid(force: boolean): Promise<boolean> | Returns boolean validation result (if field not yet validated or force is true then field is validated). | | validate(): Promise<void> | Validates field |

Field validators

Array of validators. Each validator can be string (name of validator) or function (custom validator). Allowed validator names: required or one of validator validators (e.g. isEmail). It is allowed only required validator for checkbox, select, radio and file input elements.

Field options

| Option | Type | Default | Description | |-----------------------|---------------|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------| | formElement | HTMLElement | document | Form element that contains field | | validMessage | string | '' | Message about valid value that passing to ValidationResult | | invalidMessages | object | {} | Messages about invalid value for each validator that passing to ValidationResult. E.g. {isEmail: 'Invalid Email'} | | defaultInvalidMessage | string | 'Invalid value.' | Message about invalid value that passing to ValidationResult (if not found in invalidMessages) | | validateOnChange | boolean | false | Whether to validate on change field value | | validateOnType | boolean | false | Whether to validate on typing field value | | typingDelay | number | 1000 | It is considered that user is typing when time between taps less than this value (ms) | | onTypingStart | function | null | It is triggered when user has started typing. Passes arguments: value: string, fieldElement: HTMLElement, formElement: HTMLElement | | onTypingEnd | function | null | It is triggered when user has finished typing. Passes arguments: value: string, fieldElement: HTMLElement, formElement: HTMLElement | | onChange | function | null | It is triggered when user has changed field. Passes arguments: value: string, fieldElement: HTMLElement, formElement: HTMLElement | | validatorParams | object | {} | Params for validator. (E.g. {isEmail: {allow_utf8_local_part: false}}) | | trimEnabled | boolean | true | Whether to trim field value on validation | | onBeforeValidate | function | null | It is triggered before field validation. Passes arguments: fieldElement: HTMLElement, formElement: HTMLElement | | onAfterValidate | function | null | It is triggered after field validation. Passes arguments: validationResult: ValidationResult, fieldElement: HTMLElement, formElement: HTMLElement |

Forms

Form class

| Method | Description | |--------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------| | constructor(formSelector: string, fields?: object = [], options?: object = {}) | Initializes form object with fields (see Field class) and options (see Form options) for form with formSelector selector | | listen(): void | Starts listening form for triggering validation, trimming (if enabled, for each field) and submission | | isValid(force: boolean): Promise<boolean> | Returns boolean validation result (calls isValid method for each field) | | validate(): Promise<void> | Validates form (each field) |

Form options

Form supports all Field options (besides formElement) and sets this options to each form field (Field options have higher priority).

Also Form supports following options:

| Option | Type | Default | Description | |-----------------------|------------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | onBeforeValidate | function | null | It is triggered before form validation. Passes argument formElement: HTMLElement | | onAfterValidate | function | null | It is triggered after form validation. Passes arguments: isValid: boolean, formElement: HTMLElement | | onBeforeSubmit | function | null | It is triggered when user submits form and before validation. Passes formData: FormData, formElement: HTMLElement | | submit | function | null | Submit function. It is triggered when user submits form and after validation. Must return SubmissionResult (can return Promise, so you can use AJAX). If exception occurred it is equivalent to submit returned SubmissionResult with false (error is logged to console). Passes formData: FormData, formElement: HTMLElement | | onAfterSubmit | function | null | It is triggered after submission. Passes submissionResult: SubmissionResult, formElement: HTMLElement | | onAfterSubmitSuccess | function | null | It is triggered after submission and submit function returned SubmissionResult with true. Passes submissionResultData: object, formElement: HTMLElement | | onAfterSubmitError | function | null | It is triggered after submission and submit function returned SubmissionResult with false. Passes submissionResultData: object, formElement: HTMLElement | | onBeforeFieldValidate | function | null | It is triggered before each field validation. Passes arguments: fieldElement: HTMLElement, formElement: HTMLElement | | onAfterFieldValidate | function | null | It is triggered after each field validation. Passes arguments: validationResult: ValidationResult, fieldElement: HTMLElement, formElement: HTMLElement |

To set onBeforeValidate or onAfterValdiate to each form field use onBeforeFieldValidate or onAfterFieldValidate, because onBeforeValidate and onAfterValidate are for form.

CSS classes

Special CSS classes are added dynamically to form and fields on validation and submission:

  • f-field-validating
  • f-field-valid
  • f-field-invalid
  • f-form-validating
  • f-form-valid
  • f-form-invalid
  • f-form-submitting
  • f-form-submit-success
  • f-form-submit-error

You can wrap one field by container element with js-f-field-container css class and then special classes will be added to it. This is especially useful for checkboxes and radio elements.

License

This library is released under the MIT license.