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-mandatory

v1.2.2

Published

Simple and easy Vue.js directive for form validation.

Downloads

19

Readme

vue-mandatory

Simple and easy Vue.js directive for form validation.

Steps

  1. Install it
  npm install --save vue-mandatory

  yarn add vue-mandatory
  1. Import the library and use it as Vue plugin.
  import vueMandatory from 'vue-mandatory'

  Vue.use(vueMandatory)
  
  1. Basic usage.
    • Use the directive v-mandatory in your HTML form elements.
    • The directive v-mandatory receive an object as value.
    • Mandatory each HTML form element must have an id property setted.
    • Optional params:
      • msg: String type. Is the message displayed if an element is required. If the msg is not supplied, the default value is ${elementId} is required
      • len: Number type. Is the minimum length required. Apply only for inputs. If the len is not supplied, the default value is 1.
      • pattern: Regex type. If a pattern is setted the input value must match the pattern for be valid.
      • inputClasses: Array of clases that will be apply to the input element in case of non-compliance.
      • warningClasses: Array of clases that will be apply to message element in case of non-compliance. The message element is build with an p tag and is append below the input element.
    • You can assing an object, with the custom fields, as value of the v-mandatory directive such as the email and password inputs in the following example.
    • You can assign custom values directly such as username input.
    • or only use without values such as gender select. In this way you will get the defaults values based on TailwindCSS for the styles.
 <template>
   ...
   <div class="mb-4">
    <label class="block text-grey-darker text-sm font-bold mb-2" for="username">
      Username
    </label>
    <input
      class="shadow appearance-none border rounded w-full py-2 px-3 text-grey-darker leading-tight focus:outline-none focus:shadow-outline"
      id="username"
      type="text"
      placeholder="jhonDoe"
      v-model="form.username"
      v-mandatory="{ msg: 'My custom message here', len: 5, pattern: /[a-zA-Z]{5, 10}/i inputClasses: ['error'], warningClasses: ['background-red', 'text-bold'] }"/>
  </div>
  <div class="mb-4">
    <label class="block text-grey-darker text-sm font-bold mb-2" for="email">
      Email
    </label>
    <input
      class="shadow appearance-none border rounded w-full py-2 px-3 text-grey-darker leading-tight focus:outline-none focus:shadow-outline"
      id="email"
      type="email"
      placeholder="[email protected]"
      v-model="form.email"
      v-mandatory="{ inputClasses: validate.inputClasses, warningClasses: validate.warningClasses, ...validate.email }" />
  </div>
   <div class="mb-6">
    <label class="block text-grey-darker text-sm font-bold mb-2" for="password">
      Password
    </label>
    <input
      class="shadow appearance-none border rounded w-full py-2 px-3 text-grey-darker leading-tight focus:outline-none focus:shadow-outline"
      id="password"
      type="password"
      placeholder="******************"
      v-model="form.password"
      v-mandatory="{inputClasses: validate.inputClasses, warningClasses: validate.warningClasses, ...validate.password}" />
  </div>
  <div class="mb-6">
    <label class="block text-grey-darker text-sm font-bold mb-2" for="password">
      Gender
    </label>
    <select
      class="shadow appearance-none border rounded w-full py-2 px-3 text-grey-darker leading-tight focus:outline-none focus:shadow-outline"
      id="gender"
      v-model="form.gender"
      v-mandatory>
        <option value="">Choose one</option>
        <option value="male">Male</option>
        <option value="female">Female</option>
        <option value="other">Other</option>
    </select>
    </div>
   ...
 </template>
 <script>
   export default {
     ...,
     data () {
       return  {
         validate: {
           email: {
             msg: 'The Email Field is required',
             pattern: /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/i
           },
           password: {
             msg: 'Password is mandatory',
             len: 6
           },
           gender: {
             msg: 'Choose one gender'
           },
           inputClasses: ['border-red'],
           warningClasses: ['mt-3', 'text-red', 'text-xs', 'italic']
         }
       }
     },
     ...
   }
 </script>
  1. In case you want to used in a custom component.
<!-- MyInput.vue -->
<template>
  <div class="mb-4">
    <label class="block text-grey-darker text-sm font-bold mb-2" :for="id">
      {{ label }}
    </label>
    <div class="wrap-input">
      <input
        class="shadow appearance-none border rounded w-full py-2 px-3 text-grey-darker leading-tight focus:outline-none focus:shadow-outline"
        type="email"
        v-bind="$attrs"
        v-on="$listeners"
        @input="event => $emit('input', event.target.value)"
      />
    </div>
  </div>
</template>

<script>
  export default {
    inheritAttrs: false
  }
</script>
<!-- MyForm.vue -->
<template>
  <MyInput
    id="email"
    placeholder="[email protected]"
    v-model="form.email"
    label="Email"
    v-mandatory="{ inputClasses: validate.inputClasses, warningClasses: validate.warningClasses, ...validate.email }"
  >
</template>

License

MIT © Edward S. Ramos