💪🏽 Form creation made easy, backed by state machines

Overview

Elderform

Form handling without tears and predictable form state based on defined parameters. Elderform gives you everything you need to create robust forms and stays out of your way.

npm bundle size npm npm

Features

  • Async validation
  • Create reusable forms
  • Predictable form state
  • Cancel form submission
  • Full typescript support
  • Lazy schema initialisation
  • Tiny: fully packed in just ~5kb
  • Framework agnostic
  • Ships with sensible defaults for form handling
  • No more "how do I prevent multple submission while currently submitting"

Quick start

pnpm add xstate elderform
import * as z from 'zod';
import {createForm, object} from 'elderform';

const form = createForm({
  schema: object({
    name: val => z.string().parse(val),
  }),
  onSubmit: () => {
    return Promise.resolve();
  },
});

form.subscribe((state) => {
  ...
});

form.submit();

API

createForm(config)

  • config (object) - config object from creating the form state machine (see below)

Config:

  • schema? (boolean | object) - object containing functions or false to disable schema validation
    • The schema by default is immutable, you can't change the schema after setting it initialy or by calling form.set("schema", {...}).
  • initialValues? (object) - initial form values
  • onSubmit(values: object) - an async function that handles form submission

Returns:

An object which providess

  • form.submit ((...ignore?: string[]) => void) - a function to submit the form
  • form.cancel (() => void) - function to cancel the current form submission
  • form.subscribe ((stateListener) => () => void) - a state listener with the current state of the form (see below for stateListener)
  • form.__service - the base service (xstate interpreter), made available for library authors to creating wrappers for frameworks
  • form.validate ((field) => void) - function to validate given field
  • form.set ((name, value) => void) - function to set values for data, error, errors, schema or values
  • form.setField ((name, value) => void) - function to set value of given fields in schema
  • form.spawn ((name, validator) => void) - An escape hatch to spawn new fields not specified in the schema. (useful for creating dynamic forms)
    • should be used with caution, doing this would make the form unpredictable, given you can no longer reason about your form based on the defined schema.

  • form.kill ((name) => void) - A function to kill a spawned field

State Listener

form.subscribe(currentState, handlers)

currentState

  • state - Form State

  • Boolean flags derived from form state

    • isIdle
    • isValidating
    • isSubmitting
    • isSuccess
    • submitted - similar to isSuccess
    • isError
    • validatedWithErrors - derived from validating state and if errors property is not empty
    • submittedWithoutError - derived from submitted state and if errors property is empty
    • submittedWithError - derived from error state and if errors property is not empty.
  • Others

    • values (object) - form values (Defaults to an empty object)
    • data (TData | null)
      • Defaults to undefined
      • The last data returned from successfully submission
    • error (TError | null)
      • Defaults to undefined
      • The error object last from submission, if an error was thrown
    • errors (Map<string, TErrors>) - a map containing errors for each field after validation
    • dataUpdatedAt (number) - The timestamp for when the form most recently submitted successfully and returned data (Defaults to 0)
    • errorUpdatedAt (number) - The timestamp for when the form most recently failed to submit successfully and returned error (Defaults to 0).

handlers

An object containing handlers for each field present in the schema

key type
state Field State
error TErrors
value T or null
set or setWithValidate (value: T) => void
validate () => void

Form State

  • idle - when the form isn't actively performing any operation
  • validating - when the defined schema is being validated
  • submitting - when the is being submitted
  • submitted - if the form submitted successfully without any error
  • error - if the submission attempt resulted in an error. The error is contained in the corresponding error property

Field State

  • idle - when the field is not performing any action
  • validating - when the field is validating
  • success - if the field was validated successfully
  • failed - if the field failed validation

Examples

You might also like...

Facile is an HTML form validator that is inspired by Laravel's validation style and is designed for simplicity of use.

Facile is an HTML form validator that is inspired by Laravel's validation style and is designed for simplicity of use.

Facile is an HTML form validator that is inspired by Laravel's validation style and is designed for simplicity of use.

Dec 26, 2022

The Vue form assembly tool that won't break your heart 💔

The Vue form assembly tool that won't break your heart 💔

Loveform The Vue form assembly tool that won't break your heart 💔 Loveform is a tool that helps you build validated forms in Vue 3 without the need t

Jun 10, 2022

HTML 5 & Bootstrap Jquery Form Validation Plugin

HTML 5 & Bootstrap Jquery Form Validation Plugin HTML 5 & Bootstrap 5 & Jquery 3 jbvalidator is a fresh new jQuery based form validation plugin that i

Dec 6, 2022

Simple password validator made with Javascript 💛

Simple password validator made with Javascript 💛

Password Validator Simple password validator made with Javascript 💛 Branch history base-code: a complex logic to password validator. In next branches

Jul 25, 2022

[DISCONTINUED] jQuery plugin that makes it easy to validate user input while keeping your HTML markup clean from javascript code.

jQuery Form Validator [DISCONTINUED] Validation framework that let's you configure, rather than code, your validation logic. I started writing this pl

Dec 30, 2022

:white_check_mark: Easy property validation for JavaScript, Node and Express.

property-validator ✅ Easy property validation for JavaScript, Node and Express Built on top of validator.js, property-validator makes validating reque

Dec 14, 2022

FormGear is a framework engine for dynamic form creation and complex form processing and validation for data collection.

FormGear is a framework engine for dynamic form creation and complex form processing and validation for data collection.

FormGear is a framework engine for dynamic form creation and complex form processing and validation for data collection. It is designed to work across

Dec 27, 2022

Blockchain creation path to help beginners understand block creation concept

Blockchain creation path to help beginners understand block creation concept.

Feb 17, 2022

Solidarity is an environment checker for project dependencies across multiple machines.

Solidarity is an environment checker for project dependencies across multiple machines.

Solidarity is an environment checker for project dependencies across multiple machines. When the same exact code works on one machine but not another,

Dec 11, 2022

An efficient package, which syncs ratelimits of your Discord Bot on all Machines/Clusters.

An efficient package, which syncs ratelimits of your Discord Bot on all Machines/Clusters.

Discord-cross-ratelimit An efficient package, which syncs ratelimits of your Discord Bot on all Machines/Clusters. Why? When Sharding/Scaling your Bot

Oct 23, 2022

💻 ssher.vim is interacting remote machines(only linux) through ssh connection

💻 ssher.vim is interacting remote machines(only linux) through ssh connection

Feb 21, 2022

This Login Form made using React hooks , React Js , Css, Json. This form have 3 inputs, it also validate those inputs & it also having length limitations.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Jan 3, 2022

Create a performant distributed context state by synergyzing atomar context pieces and composing reusable state logic.

Synergies Create a performant distributed context state by synergyzing atomar context pieces and composing reusable state logic. synergies is a tiny (

Nov 8, 2022

A Higher Order Component using react-redux to keep form state in a Redux store

A Higher Order Component using react-redux to keep form state in a Redux store

redux-form You build great forms, but do you know HOW users use your forms? Find out with Form Nerd! Professional analytics from the creator of Redux

Jan 3, 2023

🏁 High performance subscription-based form state management for React

🏁 High performance subscription-based form state management for React

You build great forms, but do you know HOW users use your forms? Find out with Form Nerd! Professional analytics from the creator of React Final Form.

Jan 7, 2023

Create a deep copy of a set of matched elements with the dynamic state of all form elements copied to the cloned elements.

jq-deepest-copy FUNCTION: Create a deep copy of a set of matched elements while preserving the dynamic state of any matched form elements. Example Use

Oct 28, 2022

CSS3 backed JavaScript animation framework

Move.js CSS3 JavaScript animation framework. About Move.js is a small JavaScript library making CSS3 backed animation extremely simple and elegant. Be

Dec 30, 2022

Kue is a priority job queue backed by redis, built for node.js.

Kue Kue is no longer maintained Please see e.g. Bull as an alternative. Thank you! Kue is a priority job queue backed by redis, built for node.js. PRO

Dec 20, 2022

A simple, fast, robust job/task queue for Node.js, backed by Redis.

A simple, fast, robust job/task queue for Node.js, backed by Redis.

A simple, fast, robust job/task queue for Node.js, backed by Redis. Simple: ~1000 LOC, and minimal dependencies. Fast: maximizes throughput by minimiz

Jan 5, 2023
Comments
  • Nested schema

    Nested schema

    Add support for nested schema of infinite depth. And accessing of nested fields using a dot notation e.g {a: {b: {c: {1}}}, d: {e: 2}} will be a.b.c and d.e

    opened by JoshuaAmaju 1
Releases(v1.1.2)
  • v1.1.2(Jun 1, 2022)

    • chore: add tests 954629d
    • refactor e5a7d8a
    • chore: add setting of errors on setup 8e7ecbe
    • chore: clean up e11174b
    • refactor: remove success count ed8ccfa
    • chore: actors notify parent of their entire state change ec3dc2d

    https://github.com/JoshuaAmaju/elderform/compare/v1.1.1...v1.1.2

    Source code(tar.gz)
    Source code(zip)
  • v1.1.1(May 27, 2022)

  • v1.1.0(May 24, 2022)

    • chore: add tests 9150af5
    • feat: allow validator to unset value by returning null ce30800

    https://github.com/JoshuaAmaju/elderform/compare/v1.0.3...v1.1.0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.3(May 21, 2022)

    • reset publishing eb4ecc5
    • v1.0.3 197f47a
    • chore: update doc to reflect form cancellation handler 03ec75d
    • chore: skip validation if no actor was spawned 52bef94
    • refactor: rename types and add form state to subscription value d049fbd
    • chore: add more tests 8a9ddd2

    https://github.com/JoshuaAmaju/elderform/compare/v1.0.2...v1.0.3

    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(May 20, 2022)

  • v1.0.1(May 19, 2022)

    • chore: update doc 43cdc62
    • refactor: spawn actor without initial value feb7ee4
    • chore: update doc efc18d4

    https://github.com/JoshuaAmaju/elderform/compare/v1.0.0...v1.0.1

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(May 19, 2022)

    • chore: update tests 9dfd18b
    • Merge pull request #51 from JoshuaAmaju/refactor 137fe16
    • chore: add exports 63422a3
    • chore: remove unused types cb63758
    • refactor: update build command 5198cee
    • Merge pull request #50 from JoshuaAmaju/refactor 39f13d9
    • chore: only set field value with new value if not null returned b3e752d
    • chore: remove redundant function and types 3d7e257
    • chore: update docs b3131eb
    • Merge pull request #49 from JoshuaAmaju/refactor 6b3b9e0
    • chore: update build command be859cd
    • chore: update doc d8c3985
    • refactor: update doc df8615e
    • refactor: re-write tests 2c25a67
    • refactor: remove schema definition flow c837e86
    • refactor(test): re-write actor tests c3cb425
    • refactor(readme): add nested schema to doc 4405cf8
    • change imports 190e11a
    • Merge pull request #48 from JoshuaAmaju/nested-schema 19999db
    • clean-up 1962742
    • add more test 705b636
    • add test 5314a95
    • add support for nested schema object 8135fe6
    • add schema type objects d1fc23f
    • temp a82755f

    https://github.com/JoshuaAmaju/elderform/compare/v0.2.0-0...v1.0.0

    Source code(tar.gz)
    Source code(zip)
  • v0.2.0-0(Jan 8, 2022)

    • Merge pull request #47 from JoshuaAmaju/wrapper 7d32c6b
    • switch from generating handlers from schema to spawned actors ffb8afb
    • Merge pull request #46 from JoshuaAmaju/dynamic-schema 3e21498
    • add to doc 6c10927
    • add wrapper functions for spawn and kill 807232a
    • add tests 8cb4bf9
    • add spwan and kill events guard f89bb55
    • add event to kill actor and remove field from schema ae444d5
    • add event to spawn new actor 6141656
    • add events for spawning and killing actors da9b481
    • Merge pull request #45 from JoshuaAmaju/doc/readme 3ab33c0
    • update doc 3843ec5
    • add more info about schema 7e7e243
    • remove rimraf e810454

    https://github.com/JoshuaAmaju/elderform/compare/v0.1.15...v0.2.0-0

    Source code(tar.gz)
    Source code(zip)
  • v0.1.15(Jan 5, 2022)

    • Merge pull request #44 from JoshuaAmaju/wrapper a7e4357
    • add validate field function 20aa109

    https://github.com/JoshuaAmaju/elderform/compare/v0.1.14...v0.1.15

    Source code(tar.gz)
    Source code(zip)
  • v0.1.14(Jan 5, 2022)

  • v0.1.13(Jan 5, 2022)

  • v0.1.11(Jan 5, 2022)

  • v0.1.10(Jan 5, 2022)

  • v0.1.9(Jan 5, 2022)

    • export types 83a323c
    • Merge pull request #43 from JoshuaAmaju/autoretry 490fd7d
    • add current retry count as second parameter to onRetry callback 6195e33

    https://github.com/JoshuaAmaju/elderform/compare/v0.1.8...v0.1.9

    Source code(tar.gz)
    Source code(zip)
  • v0.1.8(Jan 5, 2022)

    • Merge pull request #42 from JoshuaAmaju/wrapper 0683228
    • add more type inference 8b89700
    • add data type inference from onSubmit return type a141f85

    https://github.com/JoshuaAmaju/elderform/compare/v0.1.7...v0.1.8

    Source code(tar.gz)
    Source code(zip)
  • v0.1.7(Jan 4, 2022)

    • Merge pull request #41 from JoshuaAmaju/machine 183db34
    • clean-up 475c3d4
    • add set with validate to global events d45d654
    • Merge pull request #40 from JoshuaAmaju/doc/readme 279860c
    • Merge branch 'main' of https://github.com/JoshuaAmaju/elderform into doc/readme 1748ef5
    • add examples 14c0843
    • Merge pull request #39 from JoshuaAmaju/doc/readme 1e2b5d6
    • add shields 29c5b8f

    https://github.com/JoshuaAmaju/elderform/compare/v0.1.6...v0.1.7

    Source code(tar.gz)
    Source code(zip)
  • v0.1.6(Jan 4, 2022)

  • v0.1.5(Jan 3, 2022)

    • update test 2771566
    • update test 0eaffac
    • update test d5bf005
    • update np config 02835c4
    • fix: set field validating state 3a2f881

    https://github.com/JoshuaAmaju/elderform/compare/v0.1.4...v0.1.5

    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Jan 3, 2022)

Owner
Joshua
Joshua
Easy HTML Form Validator

Easy HTML Form Validator

Ali Nazari 314 Dec 26, 2022
Lightweight JavaScript form validation library inspired by CodeIgniter.

validate.js validate.js is a lightweight JavaScript form validation library inspired by CodeIgniter. Features Validate form fields from over a dozen r

Rick Harrison 2.6k Dec 15, 2022
Cross Browser HTML5 Form Validation.

Validatr Cross Browser HTML5 Form Validation. Getting Started View the documentation to learn how to use Validatr. Changelog Version 0.5.1 - 2013-03-1

Jay Morrow 279 Nov 1, 2022
The best @jquery plugin to validate form fields. Designed to use with Bootstrap + Zurb Foundation + Pure + SemanticUI + UIKit + Your own frameworks.

FormValidation - Download http://formvalidation.io - The best jQuery plugin to validate form fields, designed to use with: Bootstrap Foundation Pure S

FormValidation 2.8k Mar 29, 2021
jQuery form validation plugin

jQuery.validationEngine v3.1.0 Looking for official contributors This project has now been going on for more than 7 years, right now I only maintain t

Cedric Dugas 2.6k Dec 23, 2022
Simple and basic javascript form validations

JavaScript-Form-Validations Simple and basic javascript form validations Table of Validations: S. No. Type of validation Link 1 Non-empty text field h

MAINAK CHAUDHURI 23 Dec 17, 2022
Lightweight and powerfull library for declarative form validation

Formurai is a lightweight and powerfull library for declarative form validation Features Setup Usage Options Methods Rules Examples Roadmap Features ?

Illia 49 May 13, 2022
This is Example of how to use the React Hook Form

Getting Started with my-reacthook-form You can get this project by running git clone https://github.com/ivandi1980/my-reacthook-form.git Available Scr

ivandjoh 3 Apr 4, 2022
Form next js 2022 - project for death valley.

Welcome to Death Valley 2022 base url https://form-next-js-2022-3snm4xixy-sungpah.vercel.app/ Download this repo git clone https://github.com/SUNGPA

SUNGPAH 2 Jul 1, 2022