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

@vkhalikov/svelte-easy-form

v0.1.1

Published

A simple, easy to use form state and validation handler for Svelte

Downloads

5

Readme

Svelte Easy Form

A simple, easy to use form state and validation handler for Svelte.

Table of Content

Installation

npm install @vkhalikov/svelte-easy-form

Live Example

Live example with different options is available here.

Basic usage

  1. Create a form model with createForm
      import createForm from '@vkhalikov/svelte-easy-form';
       
      // Declare a schema with initial values and validators
      const schema = {
        username: {
          value: 'admin',
          validators: [required('Login is required')],
        },
        password: {
          validators: [required('Password is required')],
        },
      };
       
      const { values, errors, onInput, onSubmit } = createForm(schema);

    NOTE: Validators are not included in the package. See validator for details.

  2. Use it in your markup
    1. Create a form and pass it a submit handler. It will be called if all values are valid.

       <form on:submit="{onSubmit}" action="https://myformhandler.com" method="post">
       </form>

      If you want a custom handler, you can use submitHandler option.

    2. Create inputs.

        <form on:submit="{onSubmit}" action="https://myformhandler.com" method="post">
          <input type="text" name="username" value="{$values.username}" on:input="{onInput}" />
          <input type="password" name="password" value="{$values.password}" on:input="{onInput}" />
        </form>

      NOTE: attributes name, value, on:input are required.

      NOTE: values is a svelte store, therefore you can access it with a $ prefix.

    3. Show validation errors.

        <form on:submit="{onSubmit}" action="https://myformhandler.com" method="post">
          <input type="text" name="username" value="{$values.username}" on:input="{onInput}" />
          {#if $errors.username}
             {$errors.username}  // Render it as is
          {/if}
         
          <input type="password" name="password" value="{$values.password}" on:input="{onInput}" />
          <ValidationError error="{$errors.password}" /> // Or create a component
        </form>

      NOTE: errors is a svelte store, therefore you can access it with a $ prefix.

API

createForm

type CreateForm = (schema: Schema, options?: Options) => FormModel;

Schema

A schema is an object, that contains Fields which are used to construct a FormModel.

interface Schema {
  [fieldName: string]: Field;
}

Field

interface Field {
  value?: any; // Initial value
  validators?: [Validator];
}

NOTE: If you don't need to set an initial value and validators for a field, you should still define a Field in Schema as an empty object:

const schema = {
  name: {},
}

Validator

Validator is a function that receives a field value and returns a validation error in any form if the value is invalid or null.

type Validator = (value: any) => any;

Options

interface Options {
  validateOnInput?: boolean;
  submitHandler?: SubmitHandler;
}

validateOnInput

Defaults to true

Defines whether the field should be validated immediately after a change. As user types in a symbol for example. If set to false, the field is validated on blur and submit.

submitHandler

Defaults to undefined

type SubmitHandler = (values: Writable, event: Event) => void;

If provided, SubmitHandler will be used instead of a default browser submit handler.


FormModel

A form model that is returned from createForm function.

interface FormModel {
  values: Writable;
  errors: Writable;
  onInput: (e: Event) => void;
  onSubmit: (e: Event) => void;
}

values and errors

A Writable svelte stores, that contain current values and errors, that are accessible via $ prefix.

If you are unfamiliar with svelte stores, see the tutorial.

onInput

An event handler that updates and validates a value.

Should be passed to an input as on:input attribute.

NOTE: A value is not validated on input if the validateOnInput option is set to false.

onSubmit

An event handler that does 2 things:

  1. Validate all values for which validators were provided.
  2. If all values are valid:
    • If a SubmitHandler is provided, it will be called with the following arguments: (values, originalEvent)
    • Otherwise a default browser submit handler will be called

onSubmit should be passed to a form as on:submit attribute.

Contributing

Any feedback is welcomed. If you want to propose changes follow these steps:

  1. Fork the repo

  2. Clone the fork

  3. Create a branch - git checkout -b {prefix}/new-feature

    Prefixes: feat for features, fix for fixes

  4. Make your changes and commit git commit -a - m "short description"

  5. Push changes git push origin {prefix}/new-feature

  6. Create new Pull Request

    NOTE: Please provide a description to your changes and link an issue if it's a bugfix

Current Development State

The project is in beta, therefore anything might be changed in the future