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

ferma

v0.1.1

Published

HTML forms with less pain

Downloads

3

Readme

Ferma

Beware: alpha-quality software.

We all want to love native HTML forms — but they're tricky to work with. So, we've come up with two-way data binding to get a live form model in JS, and do all kinds of JS stuff on it instead of using native features. But what if we don't need live models, and maybe we don't even need a framework to build a decent form?

Ferma is a vanilla JS library that gives us a low-tech way to work with forms:

  • Serialize forms to JS objects.
  • Set field values from JS.
  • Work with checkbox / radio groups and multiple inputs.
  • Validate data client-side, with any rules you want.
  • Report field errors from API response with ease.
  • Notify users via DOM error messages with great UX.

And with great perks:

  • No framework required.
  • Tiny: around 1 kB of JS, min + gzip.
  • Write less code with native HTML validations.

Limitations of the current version:

  • Only supports flat form serialization (no nested objects / arrays).
  • Does not play well with front-end frameworks (field errors are not observable, does not listen to controlled input changes).
  • No multi-field live validation.

Install

npm install --save ferma

Example

Here's a simple HTML form to make a bank transfer:

<form id="transfer">
    <label>
        Bank account number
        <input name="account" required>
        <div data-ferma-error="account"></div>
    </label>
    
    <label>
        Transfer amount, $
        <input name="amount" type="number" min="0" max="1000" required>
        <div data-ferma-error="amount"></div>
    </label>
    <!-- amount hints -->
    <button type="button" id="amount-hint-full">Full amount ($1000)</button>

    <label>
        <input name="terms" type="checkbox" required>
        I accept the terms
        <div data-ferma-error="terms"></div>
    </label>
    <button>Transfer</button>
</form>

Notice it's a neat vanilla HTML form with native validations (required, min / max, etc). The only special thing here is containers with data-ferma-error attribute that will be used to display validation errors in DOM instead of the default ugly popups.

This form already works OK, but with ferma we can add a custom validation, integrate with our JSON API, show pretty error messages and apply amount hints in just a few lines of code:

import { ferma, domErrorMessages } from 'ferma';

const formElement = document.getElementById('transfer');
const fullAmountHint = document.getElementById('amount-hint-full');

const transferForm = ferma(formElement, {
    validate: {
        // validate account field
        account: v => {
            if (!v.startsWith('40817')) {
                return ferma.invalid('Account number must start with 40817');
            }
        },
    },
    submit: async () => {
        // Get a neatly serialized form value:
        alert(JSON.stringify(transferForm.getValue(), null, 2));
        // Asynchronously set field errors from API response
        transferForm.setErrors({
            account: 'account blocked'
        });
    },
});
// change input value on hint click
fullAmountHint.addEventListener('click', () => {
    transferForm.setValue({ amount: 1000 });
});
// Show validation errors in DOM containers
domErrorMessages(formElement);

Try it out in the sandbox

License

MIT License