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

@undecaf/vue-material-vuelidate

v0.3.1

Published

Using Vuelidate more easily with Vue Material

Downloads

31

Readme

Using Vuelidate more easily in Vue Material

Minified size Open issues Vulnerabilities Total downloads License

Vuelidate is a model-based validation for Vue.js that decouples JavaScript validation nicely from Vue templates. On the downside, it may lead to lengthy Vue expressions and repetitions.

The components in this package let you write concise Vuelidate validations for Vue Material input elements.

For example, an input field like this one ...

<md-field :class="{ 'md-invalid': $v.username.$error }">
  <md-input type="text" v-model="$v.username.$model" />
  <span class="md-error" v-if="!$v.username.maxLength">
    Must not be longer than {{ $v.username.$params.maxLength.max }} chars
  </span>
</md-field>

... becomes:

<md-vuelidated>
  <md-input type="text" v-model="username" />
  <md-vuelidated-msg constraint="maxLength" v-slot="{ max }">
    Must not be longer than {{ max }} chars
  </md-vuelidated-msg>
</md-vuelidated>

Here is a more extensive online example (source code) of how this package can be used.

There are more benefits than just concise markup:

Installation

As a module:

$ npm install @undecaf/vue-material-vuelidate
    or
$ yarn add @undecaf/vue-material-vuelidate

Included as <script>:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@undecaf/vue-material-vuelidate/dist/components.css">
<script src="https://cdn.jsdelivr.net/npm/@undecaf/vue-material-vuelidate/dist/components.min.js"></script>

Please note: this module needs the CSP to allow unsafe-eval (same as for the full Vue build). For your reassurance: nevertheless, no eval() inside!

Registering the components

import MdVuelidated from '@undecaf/vue-material-vuelidate'
import '@undecaf/vue-material-vuelidate/dist/components.css'
    ...
// This must come after Vue.use(VueMaterial) and Vue.use(Vuelidate):
Vue.use(MdVuelidated)

This registers <md-vuelidated> and <md-vuelidated-msg> globally.

Validating Vue Material input elements

In order to validate an <md-field>, <md-autocomplete>, <md-chips> or <md-datepicker> field (see here for providing validation messages):

  • Replace that tag with <md-vuelidated>.
  • Pass the replaced tag name as property field (defaults to md-field).
  • Place v-model on an input element (<input> or <textarea>) or on <md-vuelidated>.
  • Express constraints in the validations object of your component in the usual way.

The following examples assume that a suitable validations object has been defined.

Basic usage

Validating text:

<md-vuelidated>    <!-- becomes an <md-field> by default -->
  <label>Enter your email address</label>
  <md-input type="email" v-model.trim="mail" />
</md-vuelidated>

Validating a selection:

<md-vuelidated>
  <label>Select at most two toppings</label>
  <md-select v-model="toppings" multiple>
    <md-option value="1">Pepperoni</md-option>
    <md-option value="2">Mushrooms</md-option>
    <md-option value="3">Onions</md-option>
  </md-select>
</md-vuelidated>

Validating an autocomplete field:

<md-vuelidated field="md-autocomplete" :md-options="colors" v-model="color">
  <label>Select a color</label>
</md-vuelidated>

Validating a chips field:

<md-vuelidated field="md-chips" md-placeholder="Enter keywords" v-model="keywords">
</md-vuelidated>

Validating a date:

<md-vuelidated field="md-datepicker" v-model="dateOfBirth">
  <label>Your date of birth</label>
</md-vuelidated>

Data nesting

Just use the nested path as the v-model, e.g.

<md-vuelidated>
  <label>Homepage</label>
  <md-input type="url" v-model.trim="www.home" />
</md-vuelidated>

Collections validation with v-for

v-for can be placed on <md-vuelidated> or on a parent element. In any case, <md-vuelidated> requires a key that is bound to the v-for index.

Let's assume that comments is an array with elements like { text: 'blah' } or an object with such members. Then an input field for each comment can be rendered and validated as follows:

<md-vuelidated v-for="(_, index) in comments" :key="index">
  <label>Comment</label>
  <textarea v-model="comments[index].text"></textarea>
</md-vuelidated>

If v-for is on a parent element then <md-vuelidated> needs a copy of the key:

<div v-for="(_, i) in comments" :key="i">
    ...
  <md-vuelidated :key="i">
    <label>Comment</label>
    <textarea v-model="comments[i].text"></textarea>
  </md-vuelidated>
    ...
</div>

The v-for index should be named index. In nested v-for blocks, indices must be named index1, index2 etc.

Providing validation messages

Validation messages can be specified in two ways both of which can be combined:

  1. As the messages property of <md-vuelidated>, bound to an object containing the message for each Vuelidate constraint, e.g. :messages="{ required: 'This field is required' }".

    These messages appear below the corresponding input field.

  2. As <md-vuelidated-msg> elements, either enclosed in <md-vuelidated> tags or detached. Property constraint must be set to the Vuelidate constraint name. For detached messages, the constraint name must be qualified with the model name, see the examples below.

    Enclosed messages appear below the corresponding input field.

If multiple validations fail at the same time then only the first one (in declaration order) displays a message.

Enclosed messages

Using the messages property:

<md-vuelidated
    :messages="{ 
      required: 'Your mail address is required',
      email: 'Not a valid mail address',
    }">
  <md-input type="email" v-model.trim="mail" />
</md-vuelidated>

As <md-vuelidated-msg> tags:

<md-vuelidated>
  <label>Enter your email address</label>
  <md-input type="email" v-model.trim="mail" />
  <md-vuelidated-msg constraint="required">Your mail address is required</md-vuelidated-msg>
  <md-vuelidated-msg constraint="email">Not a valid mail address</md-vuelidated-msg>
</md-vuelidated>

Both methods can be combined.

Detached messages

Messages outside of <md-vuelidated> blocks need their constraint property to be qualified with the model name (often the collection name):

<md-vuelidated v-for="(_, index) in comments" :key="index">
  <label>Comment</label>
  <textarea v-model="comments[index].text"></textarea>
</md-vuelidated>

<!-- Detached message, referring to collection 'comments' -->
<md-vuelidated-msg constraint="comments.maxLength">Too many comments!</md-vuelidated-msg>

Accessing validator parameters

The parameters of the constraint validator are available as a v-slot:

<md-vuelidated-msg constraint="comments.maxLength" v-slot="params">
  You may not author more than {{ params.max }} comments
</md-vuelidated-msg>

Object destructuring lets us express this more elegantly:

<md-vuelidated-msg constraint="comments.maxLength" v-slot="{ max }">
  You may not author more than {{ max }} comments
</md-vuelidated-msg>

License

Software: MIT

Documentation: CC-BY-SA 4.0