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

nuxt-reactive-form

v1.2.9

Published

<a href="https://www.npmjs.com/package/nuxt-reactive-form" target="_blank">nuxt-reatctive-form</a> This module provides a composable and a bunch of validation rules to deal with forms.

Downloads

3

Readme

nuxt-reactive-form

nuxt-reatctive-form
This module provides a composable and a bunch of validation rules to deal with forms.

Installation

npm i nuxt-reactive-form

Usage

<script lang="ts" setup>

// Import validation rules by destructuring ReactiveFormValidationRules
const {
  required,
  minLength
} = ReactiveFormValidationRules

// Or you can create yours
const onlyThisOrThat = (params?: { message?: string }) => {
  return ({ value, state }) => {
    return value === 'This' || value === 'That' || params?.message;
  }
}

// Instantiate reactive form from an object
const form = useReactiveForm({

  // Init with a single value
  // [key: string]: any|Ref<any>
  // No validation or custom resetting. By default, reset to the initial value
  firstname: 'John',

  // Or init with an object:
  // [key: string]: {
  //   value: any|Ref<any>,
  //   reset?: any|Ref<any>, if omitted, the value will be reset to the initial value,
  //   validation?: TValidationRule[], an array of validation rules imported/created above. If omitted, no validation
  // }
  lastname: {
    value: 'Doe',
    reset: 'Something else',
    validation: [
      required(), // Some rules don't need any param. A default error message will be displayed if rule is not verified
      minLength({ message: 'This field must contain at least {min} characters', min: 3 }), // You can use placeholders matching other params keys in your message
      onlyThisOrThat({ message: 'This field must contain the values "This" or "That"' }) // Or use your own rules
    ]
  }

});

// Destructure the instance of reactive form.
// See "useReactiveForm return values" below for more informations
const {
  state,
  resetForm,
  formIsValid,
  getError,
  forHasChanged
} = form;

const submit = () => {
  if(formIsValid()) {
    console.log('Form is valid! :D');
    resetForm();
    return;
  }
  console.log('Form is invalid... :(');
}

</script>

<template>

  <h1>Testing ReactiveForm</h1>

  <form>

    <div>
      <input
        type="text"
        v-model="state.firstname.value"
      >
      <template v-if="getError('firstname')">
        <p
          style="color: red"
          v-for="(error, index) in getError('firstname')"
          :key="index"
        >{{ error }}</p>
      </template>
    </div>

    <div>
      <input
        type="text"
        v-model="state.lastname.value"
      >
      <template v-if="getError('lastname')">
        <p
          style="color: red"
          v-for="(error, index) in getError('lastname')"
          :key="index"
        >{{ error }}</p>
      </template>
    </div>

    <button @click.prevent="submit()">Submit</button>

    <button @click.prevent="resetForm()">Reset</button>

  </form>

</template>

useReactiveForm return values

An instance of useReactiveForm returns the following values.

state

The current state of form values. It can be used by example in v-model:

<input
  v-model="state.whatever.value"
>

errorMessages

The collection of error messages depending on the validation rules.

resetForm()

A method that allows to reset form to initial values or reset values if specified in initialization object.

validateState(stateKey: string)

A method that allows to validate a specific form value. It can be used by example in events:

<input
  v-model="state.whatever.value"
  @change="validateState('whatever')"
>

validateForm()

A method that allows to validate all form values.

formIsValid(validate: boolean = false)

A method that returns true or false whether all form values are valid or not, after validating form by running validateForm() whether validate is set to true or false (false by default).

getError(key: string)

A method that returns all error messages for a specific value, or false if there isn't any. It can be used by example like this:

<input
  v-model="state.whatever.value"
  @change="validateState('whatever')"
>
<template v-if="getError('lastname')">
  <p
    style="color: red"
    v-for="(error, index) in getError('lastname')"
    :key="index"
  >{{ error }}</p>
</template>

formHasChanged

A computed returning true or false whether any form value has changed or not.