Socket
Socket
Sign inDemoInstall

@casthub/form

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @casthub/form

Vue 3 Utility Hook for Form handling and validation


Version published
Maintainers
1
Created

Readme

Source
NPM Discord Apache-2.0

Form Handling and Validation Hook for Vue 3

@casthub/form provides Vue 3 Hooks for consuming, validating and managing Forms.

Inspired by vue-hooks-form.

  • Asynchronous validation via async-validator
  • No forced HTML structure/format
  • Error handling
  • TypeScript

Installation

This package is available via NPM:

yarn add @casthub/form

# or

npm install @casthub/form

Usage

A full example App is provided in the example folder.

<template>
    <form @submit.prevent="submit">
        <label for="email">Email Address</label>
        <input
            type="email"
            name="email"
            id="email"
            placeholder="Email Address"
            :disabled="loading"
            @focus="email.clearError"
            v-model="email.value"
        />
        <p class="err" v-if="email.error">{{ email.error.message }}</p>
        <label for="email">Password</label>
        <input
            type="password"
            name="password"
            id="password"
            placeholder="Password"
            :disabled="loading"
            @focus="password.clearError"
            v-model="password.value"
        />
        <p class="err" v-if="password.error">{{ password.error.message }}</p>
        <button
            type="submit"
            :disabled="loading"
        >Login</button>
    </form>
</template>

<script lang="ts" setup>
    import { useForm } from '@casthub/form';

    const { useField, handle, loading } = useForm<{
        email: string;
        password: string;
    }>({
        defaults: {
            email: 'hello@example.com',
        },
    });

    const email = useField('email', {
        type: 'email',
        required: true,
    });
    const password = useField('password', {
        required: true,
    });

    const submit = handle(async ({ email, password }) => {
        alert(`Email: ${email} Password: ${password}`);
    });
</script>

API

useForm

Options
OptionDefaultTypeDescription
defaults{}Record<string, any>Optionally provide defaults for the various fields in this object by key -> value pairs.
validationModesubmit'change'|'submit'NOT IMPLEMENTED YET. Whether to validate input once submitted
Response
OptionTypeDescription
useFieldField<unknown>Documented below.
handle(run: values => Promise<void>) => async (e?: Event) => Promise<void>Registers the asynchronous handler that runs once a form is submitted and successfully validated.
reset() => voidReset the Form to tis default state.
validate() => Promise<boolean>Manually trigger validation and error handling.
clearErrors() => voidClear all errors for all fields.
loadingRef<boolean>Whether the form is currently executing.
destroy() => voidDestroy and clean-up the Form handler. Happens automatically during onBeforeUnmount.

useField

Options

Currently the options object provided to useField is inheritted from async-validator and all options are forwarded as a validation field.

Response
OptionTypeDescription
errorsValidateError[]An array of all Errors set against this Field.
errorValidateError|nullOptimistically picks one, if any, of the Errors against the field.
hasErrorComputedRef<boolean>Whether or not the Field has 1 or more errors.
setError(text: string) => voidManually set the error on this field.
clearError() => voidClears all Errors currently set against this Field.
valueWritableComputedRef<unknown>The value for the field, compatible with v-model.

To-do

  • Add a test suite

Keywords

FAQs

Last updated on 28 Jul 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc