Git Product home page Git Product logo

svelte-headless-form's Introduction

Svelte Headless Form

A fast, light and batteries-included form library to make you more productive.

Getting Started

npm install svelte-headless-form

How to use

<script>
    import { createForm } from 'svelte-headless-form';
    const { submitForm, input, errors, values, register } = createForm({
        validateMode: 'onBlur', // Defaults - Schemaless:onChange Schema:onBlur
        initialValues: {
            username: '',
            password: ''
        },
        initialValidators: {
            username: (value) => (value.length > 0 ? false : 'Name is required'),
            password: (value) => (value.length > 0 ? false : 'Password is required')
        }
    });
</script>

<form on:submit|preventDefault={submitForm((values) => console.log(values))}>
    <input
        type="text"
        bind:value={$values.username}
        use:register={{ name: 'username' }}
    />
    {#if $errors.username}
        <div>
            {$errors.username}
        </div>
    {/if}

    <input
        type="password"
        bind:value={$values.password}
        use:register={{ name: 'password' }}
    />
    {#if $errors.password}
        <div>
            {$errors.password}
        </div>
    {/if}

    <button type="submit">Submit</button>
</form>

Philosophy

Svelte Headless Form allows for 2 different validation implementations, called schema-based validation and schemaless validation. In the How To Use section we are demonstrating schemaless validation by giving each form value it's own validator in the initialValidators prop. If you are intereseted in using schema based validaton please give your createForm() a prop called 'validationResolver' which is a single function that returns an object with error strings located at the same path of the corresponding values. In the future we plan to have pre-built validation resolvers for all the major schema based validators like zod, yup and joi to name a few.

Roadmap

These roadmap features are not ordered by priority.

  • Support schemaless validation updates after form creation.
  • Support validation dependencies.
  • Update README with more advanced examples.
  • Create a website with a tutorial, an API overview and documentation.
  • Send through entire form state to schemaless validators.
  • Support async schemaless validators.
  • Support schema-based validation.
  • Unify useField and useFieldArray api by passing down control.
  • Support a revalidateMode in createForm options.
  • Explore simpler options for attaching handleChange, handleBlur and handleFocus events to inputs.

Please consider svelte-headless-form in beta until a 1.0 release.

svelte-headless-form's People

Contributors

nickt26 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.