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-forms-mixin

v0.4.2

Published

A helper package for VueJS 2 to manage forms validation and messages

Downloads

6

Readme

vue-forms-mixin

What it does

vue-form-mixin is a simple mixin for vuejs 2 that helps handling form validation and error messages.

Install

npm install --save vue-forms-mixin

Import

import FormMixinMaker from 'vue-forms-mixin';

Configure

You basically create the form mixin by calling the FormMixinMaker with configuration parameters:

const formMixin = FormMixinMaker({
    classSuccess: 'success',
    classError: 'error',
    validationDefinitionPrefix: 'validationDefinition',
    validationReferencePrefix: 'validationReference',
    validateRequiredMessage: 'This field is required',
});

Overriding classes

You surely have to override add your error and success classes to your inputs:

  • classSuccess: class to be appended to input if value is valid
  • classError: class to be appended to input if value is invalid

Overriding error messages

xxxMessage: message to append to validation status in case of error for validation method 'xxx' xxx is the name of the validation method whose error message you want to override.

Overriding data attributes

You should not have to do this... but maybe you like shorter syntaxes...

  • validationDefinitionPrefix: prefix for validation definition (data-validation-definition="[definition]")
  • validationReferencePrefix: prefix for validation reference (data-validation-reference="[reference]")

Basic Use

  • Add the mixin you created to the mixins list of any component
  • Use data-validation-reference and data-validation-defintion to bind validation to an input
  • use getValidationError(reference) to display error messages

The mixin will bind validation to any html element with a data-validation-definition attribute. The syntax is validationMethod(param1, param2, param3). The data-validation-reference attribute is used to bind result to a reference, this reference can be used afterwards to access validation attributes in the validation object (see below) or when using the getValidationError method. By default the reference is the first dependency detected.

Check the test component for a complete example. Or see below for a quickie.

Exemple:

<template>
  <input type="text"
        v-model="user.name"
        data-validation-reference="user"
        data-validation-definition="validateRequired(user.name)"/>
        {{ getValidationError('user') }}
</template>
<script>
import FormMixin from '@/framework/mixins/formMixin';
const formMixin = FormMixinMaker({
/* Your config goes there */
});

export default {
  name: 'Profile',
  mixins: [formMixin],
  props: ['user'],
}
</script>

Advanced use

The mixin will generate a computed property called validation whose attributes are:

  • valid: boolean indicating whether all inputs of the form are valid
  • inputs: an array of validation information (one element per input). The validation information contains the following attributes:
    • valid: boolean indicating whether the input is valid or not
    • error: the error message pertaining to the validation method
    • method: the validation method
    • params: the compiled parameters passed to the validation method

Use this property however you like to do whatever you please.

Existing validation methods:

  • validateEmail(email): Validates 'email' is an email address
  • validateMinChars(text, minChars): Validates that 'text' has at least 'minChars' characters
  • validateRequired(text): Validates that 'text' has at least one char
  • validateGreaterThan(number, min): Validates that 'number' is greater than 'min'
  • validatePositive(number): Validates that 'number' is greater then zero
  • validateMultipleOf(number, mod): validates than 'number' is a multiple of 'mod'
  • validateEqual(text1, text2): Validates that 'text1' equals 'text2'

FAQ

Can I add my own validation functions

Yes, just drop the validation functions in your component either directly or using your own mixin. Please note that your function signature must be function validateCustom(model, params) where params will be an array containing the arguments passed in the validationDefinition attribute. You can also add error messages for your custom functions using the configuration object.

What happens if I modify a value used as a parameter to a validation method

The mixin tracks dependencies, so it should magically work anyways... It should...

Is there anyway to know if the form or attributes are dirty/pristine and prevent pristine inputs to have error/success classes binded to them?

I am thinking to do it. Lemme know if you would like to have it implemented