1.0.8 • Published 2 years ago

svelte-better-form v1.0.8

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

svelte-better-form

A form management library for Svelte that is very lightweight and simple!;

npm GitHub Repo stars

Why svelte-better-form?

  • Very easy, fast, lightweight and powerful!
  • Has validation already!

Create form

// Simplest
const { values } = betterForm({ name: "better" });

// Full example!
const { values, loading, errors, submit, getValue, setValue } = betterForm(
  {
    name: "",
    email: "",
    age: null,
  },
  {
    validators: {
      name: [
        requiredValidator("Name is required!"),
        minLengthValidator(8, "Name should be longer than 8!"),
      ],
      email: emailValidator("Must be email!"),
      age: (value) => {
        // you can validate [age] here manually.
        if (value < 10) {
          // return a string if it is invalid.
          return "Your age must be older than 10";
        }
      },
    },
    onSubmit: async () => {
      // When you call submit, this one will be executed if the form is valid.
    },
  }
);

Use Form

<!-- Bind value -->
<input type="text" bind:value="{$values.name}" />

<!-- Use errors  -->
{#if $errors.name}
<p>Error: {$errors.name}</p>
{/if}

<!-- Use loading -->
<button disabled="{$loading}" class="button" on:click="{submit}">Submit</button>

Thats all!

The default validators

  • minLengthValidator(minLength, errorMessage)
  • maxLengthValidator(maxLength, errorMessage)
  • requiredValidator(errorMessage)
  • emailValidator(errorMessage)

Custom Validator

parameterName: (value) => {
  if(value !== "whatYouWant"){
    return "error text"
  }
 },
1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago