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

vue-use-form-state

v1.0.5

Published

A composable utility for encapsulating form state and validation logic in Vue applications.

Downloads

437

Readme

vue-use-form-state

A composable utility that simplifies encapsulation and managing form state and validation logic in Vue applications. It provides real-time validation feedback by reactively updating the form’s validity status when form data changes.

Key Features

  • Reactive validation: The validity state automatically updates when the form data changes.
  • Built-in form state management: Manage form data, errors, and warnings with a simple API.
  • Customizable validation logic: Encapsulate you validation rules with a flexible validator function.
  • Deep validation: Supports validation for deeply nested form data and error structures.

Basic Usage

In a form component:

import { useFormState } from 'vue-use-form-state';

const { data, errors, validate, reset } = useFormState({
  initialData: {
    name: '',
  },
  validator(data, errors, warnings) {
    if (data.name.trim().length === 0) {
      errors.name = 'Please enter a name';
    }
    if (data.name.length > NAME_MAX_LEN) {
      errors.name = `Name cannot exceed ${NAME_MAX_LEN} characters`;
    }
    // Any other validation logic...
  },
});

function handleClose() {
  reset();
  // Additional logic on form close...
}

function handleSubmit() {
  // Validate the form before submission
  if (!validate()) {
    return;
  }
  // Proceed with form submission logic when the form is valid...
}

<MyInput
  value={data.value.name}
  onChange={(value) => (data.value.name = value)}
  validity={errors.value.name ? 'error' : 'none'}
  caption={errors.value.name && <MyErrorCaption>{errors.value.name}</MyErrorCaption>}
/>

In this example:

  • data holds the form's reactive state.
  • errors holds the form's reactive validity state.
  • validate() triggers validation logic.
  • reset() resets the form to its initial state.

Options

  • initialData: Object The starting values for your form. These values will be passed to the validator during the first call to validate().

  • initialErrors: Object (optional) Initial state for form errors. You can provide a default state that will be passed to the validator.

  • initialWarnings: Object (optional) Similar to initialErrors but for non-critical warning messages.

  • validator(data, errors, warnings): Function A custom function where you define validation logic. It runs when validate() is called and reactively updates as data changes. Use this to assign error and warning messages to specific fields.

State

  • data: Object The form data being validated. Any changes to this object will trigger reactive revalidation but only after validate() is invoked at least once.

  • errors: Object Stores validation errors set within the validator function. Access this to display error messages in your form.

  • warnings: Object Holds non-fatal warning messages from the validator.

  • hasErrors: Boolean A reactive flag that indicates whether there are any errors in the form.

Methods

  • validate(): boolean - Triggers the initial validation and enables reactive validation for subsequent changes to data. Returns value of hasErrors described above.

  • reset(data?: Object) - Resets validation state, resets data to initialData and stops reactive validation updates. Optional data argument can be used to overwrite certain fields of initialData.

Note:

The validation works with deeply nested form data and error structures. It checks whether the errors and warnings objects contain any non-empty values after executing validator function.

For example:

// Considered invalid and `hasErrors` will be true:
errors: {
  user: {
    name: 'Please enter a name'
  }
}
// Considered valid and `hasErrors` will be false:
errors: {
  user: {
    name: '',
    phoneNumbers: []
  }
}