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

@resourge/vue3-hook-form

v1.5.0

Published

vue3-hook-form is a simple and basic controlled hook form. Aiming to create forms with minimal effort.

Downloads

413

Readme

Vue 3 Form Hook

License

Description

useForm is a custom hook designed to facilitate form state management and validation in Vue 3 applications. This documentation provides a comprehensive guide on how to integrate and utilize useForm effectively within your project.

Table of Contents

Installation

To install use npm:

npm install @resourge/vue3-hook-form

or with Yarn:

yarn add @resourge/vue3-hook-form

Usage

import { useForm } from 'vue3-hook-form';

Defining Form Data Model and Validation Schema

import { object, string } from '@resourge/schema';

// Define the shape of your form data
export class LoginUserFormModel {
  public username = '';
  public password = '';

  toModel() {
    return {
      username: this.username,
      password: this.password,
    };
  }
}

// Define a schema for form validation
const schema = object<LoginUserFormModel>({
  username: string().notNullable().required('Username is required'),
  password: string().notNullable().required('Password is required')
});

Creating a Form Instance

export const useLoginForm = () => {
  return useForm<LoginUserFormModel>(new LoginUserFormModel(), {
    validate: (form) => {
      return schema.validate(form);
    },
    validateDefault: true
  });
};

Using the Form Instance in Vue Component

<template>
  <form @submit.prevent="onSubmit">
    <FormControl :label="T.pages.login.form.username" :errors="getErrors('username')">
      <Input v-model="form.username" type="text" />
    </FormControl>
    <FormControl :label="T.pages.login.form.password" :errors="getErrors('password')">
      <Input v-model="form.password" type="password" />
    </FormControl>
    <div class="form-actions">
      <Button :label="T.pages.login.actions.enter" type="submit" variant="primary" />
    </div>
  </form>
</template>

<script>
import { useLoginForm } from '../modules/useForm.ts';

export default {
  setup() {
    const { form, handleSubmit, getErrors } = useLoginForm();

    const onSubmit = handleSubmit(async () => {
      // Your form submission logic here
    });

    return {
      form,
      onSubmit,
      getErrors
    };
  }
};
</script>

FormState

FormState represents the state of a form managed by useForm. It offers various methods and properties to interact with and manage the form's state and data.

Properties and Methods

  • changeValue(key, value): Updates the value of a specific field in the form.
  • errors: An object containing validation errors for the form fields.
  • field: Options and configurations for the form fields.
  • form: Representation of the current state of the form data.
  • getErrors(key): Get the validation errors for a specific field in the form.
  • getValue(key): Get the current value of a specific field in the form.
  • handleSubmit(onSubmit): Handles form submission.
  • hasError(key): Checks if a specific field in the form has validation errors.
  • isValid: Indicates whether the form is currently in a valid state.
  • onChange(key): Attaches an event listener to a specific form field.
  • reset(newForm): Resets the form to a specified state.
  • setError(error): Sets a validation error for the form or a specific field.
  • watch(callback): Registers a callback function to watch for changes in the form's state.

Support for Complex Forms

useForm supports complex forms such as wizards, multi-step, and nested state. Here's an example of defining a nested form:

// Define the shape of your form data
export class UserAddressModel {
  public street = '';
  public city = '';
  public postalCode = '';
}

export class UserProfileModel {
  public firstName = '';
  public lastName = '';
  public address = new UserAddressModel();
}

// Define a schema for nested form validation
const addressSchema = object<UserAddressModel>({
  street: string().notNullable().required('Street is required'),
  city: string().notNullable().required('City is required'),
  postalCode: string().notNullable().required('Postal code is required')
});

const userSchema = object<UserProfileModel>({
  firstName: string().notNullable().required('First name is required'),
  lastName: string().notNullable().required('Last name is required'),
  address: addressSchema // Nested validation schema
});

// Create a form instance with default values and validation
export const useUserProfileForm = () => {
  return useForm<UserProfileModel>(new UserProfileModel(), {
    validate: (form) => {
      return userSchema.validate(form);
    },
    validateDefault: true
  });
};

For more detailed usage instructions, refer to the documentation.

Documentation

For comprehensive documentation and usage examples, visit the Vue 3 Form Hook documentation.

Contributing

Contributions to Vue 3 Form Hook are welcome! To contribute, please follow the contributing guidelines.

License

Vue 3 Form Hook is licensed under the MIT License.

Contact

For questions or support, please contact the maintainers: