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

reactive-vue-form

v1.0.148

Published

```js const initialValue = null; const control = new FormControl(initialValue); ``` #### usage in html ```html <input type="text" v-model="control.value" /> ```

Downloads

10

Readme

Simplest way to validate yours vue forms

DOCS

https://todos-form-production.up.railway.app/

FormControl

create control

const initialValue = null;
const control = new FormControl(initialValue);

usage in html

<input type="text" v-model="control.value" />

FormGroup

const formGroup = new FormGroup({
  name: new FormControl(),
  age: new FormControl(),
  tosos: new FormArray([]),
});

usage in html

  <div class="d-flex">
    <div>{{ formGroup.name.value }}<div>
    <div>{{ formGroup.age.value }}<div>
  </div>
  <template v-for="control in formGroup.todos.controls">
    <template v-if="control instanceof FormControl">
      <textarea v-model="control.value"></textarea>
    </template>
    <template v-if="control instanceof FormGroup">
      <input type="checkbox" v-model="control.status.value" />
      <textarea v-model="control.name.value"></textarea>
    </template>
  </template>

add child

// add new todo with status
formGroup.todos.addControl(new FormGroup({
  status: new FormControl(false),
  name: new FormControl(''),
}));

// or simple todo with only name
formGroup.todos.addControl(new FormControl('todo name')),

// or both
formGroup.todos.addControls([
  new FormGroup({
    status: new FormControl(false),
    name: new FormControl(''),
  }),
  new FormControl('todo name')
]);

FormArray

const formArray = new FormArray([
  new FormControl(),
  new FormArray([
    new FormControl(),
  ]),
  new FormArray([
    new FormGroup({
      control: new FormFormArray([]),
    }) 
  ]),
]);

usage in html

  <template v-for="control in formArray.controls">
    <template v-if="control instanceof FormControl">
      <textarea v-model="control.value"></textarea>
    </template>
    <template v-if="control instanceof FormArray">
      <render-array :array="control.controls" />
    </template>
  </template>

Validation

There are two ways to validate data

  1. synchronous functions
  2. asynchronous functions

synchronous functions

FormControl

const initialValue = null;
// generic type for control
function validator<FormControl>(control) {
  if (control.value != 1) {
    // if not valid
    return { 'one': 'value must be 1' }
  }
  // if valid
  return null
}
const control = new FormControl(initialValue, [validator]);

FormGroup

function checkTodoStatus<FormGroup>(control) {
  const todos = control.get<FormArray>('todos');
  if (todos.controls.every(control => control instanceof FormGroup)) {
    return todos.value.every(value => value.status === true) ? null : { 'errorStatus': 'all must be completed' }
  }

  return null;
}

const formGroup = new FormGroup({
  name: new FormControl(),
  age: new FormControl(),
  tosos: new FormArray([
    new FormGroup({ status: new FormControl(false), name: new FormControl() }),
    new FormGroup({ status: new FormControl(false), name: new FormControl() }),
    new FormGroup({ status: new FormControl(false), name: new FormControl() })
  ]),
}, [checkTodoStatus]);

FormArray

function controlsLength<FormArray>(control) {
  if (control.controls.length < 2)) {
    return { 'controlsLength': 'invalid length' }
  }

  return null;
}

const formGroup = new FormArray({
  new FormControl(),
  new FormControl(),
}, [controlsLength]);

asynchronous function

Everything is exactly the same as with synchronous validators, only the validator must return a promise

/*
  abortController - https://developer.mozilla.org/en/docs/Web/API/AbortController.
  It can be used to cancel requests.
  If the form is updated, then all active asynchronous validators will be canceled and the method will be called
  abortController.abort(); 
  dont call abortController.abort() inside validator just return error object or null
  
  
  validator must return a promise or be an asynchronous function
*/

function asyncValidator<FormControl>(control, abortController): Promise<ValidationErrors> {
  return fetch(url, {
    signal: abortController.signal
  }).then((response) => {
    // if not valid
    if (response.error) {
      return {
        return { 'httpError': response.errorMessage }
      }
    }
    
    // if valid
    return null;
  });

}
const control = new FormControl(initialValue, [/*sync validators*/], [asyncValidator]);

property

pending

Allows you to control the processes of synchronous verification If it returns true, then 1 or more asynchronous validators in progress