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

@mindscreen/formvalidation

v0.1.3

Published

Provides means for an accessible form validation

Downloads

47

Readme

mindscreen Accessible Form Validation

npm

This package implements an accessible form validation.

Core features

  • Fields are validated when leaving the input and error messages are displayed inline.
  • When submitting the form, an error summary is displayed above the form
  • Extensible validation and error rendering

Screenshot of a form with inline errors and error summary above

Usage

npm install @mindscreen/formvalidation
# or
yarn add @mindscreen/formvalidation

Concepts

  • The Validation describes how your form validation is supposed to work. It configures the validators used and how the errors should be handled.

  • A Validator adheres to the ValidatorInterface and determines whether a form input is valid. In case of an error, it may provide an error message and additional arguments to provide information on the error (e.g. allowed vs. actual string length).

  • The ErrorHandler will handle how the errors are displayed in your form. If not specified, the default error handler will be used.

  • Finally, the ValidationInstance is this whole configuration applied for a single form element with Validation.init(form, options). It provides an interface to trigger validation or clear validation errors, but also knows how to retrieve the elements in your form. This can be configured in the options to match your template.

    • An InputGroup usually is a (layout) container around a form element, its label and possibly its validation messages. They are accessed as ValidationInstance.inputGroups and are the cornerstone of input validation: all validation is based on these groups.
    • Inputs and their related content (like labels) is derived from the InputGroup (see InputValidation.getInput, InputValidation.getLabel).

    See the example templates for the HTML structure expected in the default options.

Browser Validation

There is a NativeValidator using the browser APIs for form element validation. It requires the least setup, as the browser will provide localized error messages. Note however that these might not be in the language of your web page.

See examples/nativeValidation.ts.

You can add additional validators, but all applicable validators are evaluated, i.e. you might get both native and custom validator handling. You can of course extend the validator and override the applicability condition:

import { NativeValidator } from '@mindscreen/formvalidation/src/validation/validators';

const MyNativeValidator = {
    ...NativeValidator,
    isApplicable: input => NativeValidator.isApplicable(input)
        && !input.hasAttribute('data-custom-validation'),
};

Custom Validation

See examples/fromOptions.ts on how to completely set up your custom validation.

If you encode all translations and configuration in your markup, you can have a look at src/default-setup.ts, which provides a basic setup.

Custom Validators

To allow for custom rules, you can implement custom validators. See examples/customValidator.ts.

Build

Build the examples

npm install
npm run examples

Build the docs

npm install
npm run docs

Build distribution code

npm install
npm run build