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

handerrors

v1.1.0

Published

get errors from back-end for validate form

Downloads

13

Readme

handerrors

npm version install size npm downloads

a simple library for handing back-end errors with front-end worked with angular,react and vuejs and native js

for exemple if you have a api you want sending data by httpClient you have to validate data in back-end and you have using framework like laravel if not valide will return a json errors something like you have to hand this errors in check if filed hasErrors or not and return errors if field hasErrors

{
    message:'invalid data',
    errors :{
            firstName:['error 1','error 2'],
            lastName:['error 1','error 2'],
    }
}

Install

npm install handerrors --save-dev or use cdn

``

methods api

  • handErrors(errors={}) // helpers return a object HandErrors
  • setErrors(errors) // set new errors to prop errors setErrors({firstName:['error 1','error 2']})
  • hasErrors(field) // return true if giving field is has errors hasErrors('firstName')
  • getErrors(field) // return array|string of field if exist else null
  • error(field) // return object with HasError/getErrors
  • controls // get field with errors exemple controls.firstName.errors/ controls.firstName.hasErrors // controls.firstName.remove();

How works ??


import handErrors from 'handerrors';
import axios from 'axios';

...

let erros  = handErrors();
errors.setErrors(
{
    message:'invalid data',
    errors :{
            firstName:['error 1','error 2'],
            lastName:['error 1','error 2'],
    }
}
);
if(errors.controls.someField.hasErrors){
// field has errors
  console.log(errors.controls.someField.errors)
} else {
  // not errors
}

Exemple with vuejs / laravel

<template>
   <div class="form-group">
        <label for="first_name">First Name</label>
        <input
          v-model="formdData.model.first_name"
          @blur="contorls.first_name.remove()"
          :class="{'is-invalid':contorls.first_name.hasErrors}"
          type="text"
          class="form-control"
          id="first_name"
          placeholder="Last Name"
          
        />
        <div v-if="contorls.first_name.hasErrors" class="error">
          <p v-for="msg in contorls.first_name.errors" :key="msg">{{msg}}</p>
        </div>
      </div>
     <div class="form-group">
        <label for="last_name">Last Name</label>
        <input
          v-model="formdData.model.last_name"
          @blur="contorls.last_name.remove()"
          :class="{'is-invalid':contorls.last_name.hasErrors}"
          type="text"
          class="form-control"
          id="last_name"
          placeholder="Last Name"
          
        />
        <div v-if="contorls.last_name.hasErrors" class="error">
          <p v-for="msg in contorls.last_name.errors" :key="msg">{{msg}}</p>
        </div>
      </div>
      <div class="form-group">
        <label for="username">=UserName</label>
        <input
          v-model="formdData.model.username"
          :class="{'is-invalid':contorls.username.hasErrors}"
          @blur="contorls.username.remove()"
          type="text"
          class="form-control"
          id="username"
          placeholder="UserName"
        />
        <div v-if="contorls.username.hasErrors" class="error">
          <p v-for="msg in contorls.username.errors" :key="msg">{{msg}}</p>
        </div>
      </div>
      <div>
       <div class="form-group">
        <label for="password">=password</label>
        <input
          v-model="formdData.model.password"
          :class="{'is-invalid':contorls.password.hasErrors}"
          @blur="contorls.password.remove()"
          type="text"
          class="form-control"
          id="password"
          placeholder="password"
        />
        <div v-if="contorls.password.hasErrors" class="error">
          <p v-for="msg in contorls.password.errors" :key="msg">{{msg}}</p>
        </div>
      </div>
      <div>
        <button @click="createUser()" type="button" class="btn btn-primary">
        <div v-if="formData.submited" class="spinner-grow text-warning" role="status">
          <span class="sr-only">Loading...</span>
        </div>
        <span v-else>Register</span>
      </button>
</template>
<script>
import handErrors from 'handerrors'
export default {
  mounted() {
    this.init();
  },
  data: () => {
    return {
      formData: {
        model: {
          username: "",
          first_name: "",
          last_name: "",
          password: ""
        },
        errors: {},
        submited: false,
      }
    };
  },
  methods: {
    createUser() {
      this.formData.submited = true;
      axios.post('endpoint').then(
        res => {
           this.formData.submited = false;
            aler("ok");
        },
        err => {
          this.formData.submited = false;
          this.formData.errors = handErrors(err.body.errors);
        }
      );
    },
    controls(){
    return this.formData.errors;
  },
  
};
</script>

Contribute

feel free to Contribute a edit code we can make better