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-form-component

v1.0.10

Published

Simple Vue form component with validation options

Downloads

17

Readme

vue form component

A simple vue component that can wrap forms with validation and submit functionality.

installation

Using npm:

$ npm i -save-dev vue-form-component

usage

set up

import and use as a component in your javascript:

import VueForm from 'vue-form-component';
import { required } from 'vue-form-component/src/validators';

let vm = new Vue({

    el: '#app',

    data: {

        forms: {

            formslug: {

                fieldname: {
                    rules: {
                        required
                    },
                    messages: {
                        [required.fieldname]: 'error message ... blah blah blah'
                    }
                },
            },
        }
    },

    components: {
        VueForm
    },

    methods: {

        onFailure(error) {
            
            // handle an unsuccessful form submission here
            
        },

        onSuccess(response) {
            
            // handle the successful form submission here
            
        }
        
    }

});

use custom component in your html:

<vue-form method="post"
          action="/the/form/action"
          slug="formslug"
          v-on:success="onSuccess"
          v-on:failure="onFailure"
          ref="formref">

    <form action="/the/form/action"
          method="post"
          @submit.prevent="onSubmit"
          @keydown="dataset.errors.clear($event.target.name)">

        <div class="form-group">
            <input type="text" class="form-control required" id="fieldnameInput" placeholder="" name="fieldname"
                   v-model="dataset.fieldname"
                   v-bind:class="{ 'is-invalid': dataset.errors.has('fieldname') }">
            <span class="invalid-feedback" role="alert" v-if="dataset.errors.has('fieldname')">
                <strong v-text="dataset.errors.get('fieldname')"></strong>
            </span>
        </div>

        <div class="form-group row align-items-center">
            <div class="col-12 col-lg-5 text-center text-lg-right mt-3 mt-lg-0">
                <button type="submit" class="btn btn-orange mt-5 mt-md-0 mb-0">submit</button>
            </div>
        </div>

    </form>

</vue-form>

the form component uses the form-data-manager package, in order to handle form validation and submission. Upon initialization, it will look for a set of rules at it's parent component following a look at the root vue component using it's slug property as reference.

<vue-form inline-template
          ...
          slug="formslug"
          ...
          >

means the component will be looking for the parent component data property this.$root.$data.forms.formslug or for root component this.$root.$data.forms.formslug property as below

let vm = new Vue({

    data: {

        forms: {

            formslug: {

                //...
            },
        }
    },

    //...

});

form validation

vue-form-component will handle validation of fields found in a form, by order of the rules assigned to it. Available rules can be imported by the component's validators directory.

import { 
    required, 
    integer, 
    //...
} from 'vue-form-component/src/validators';

more rules wil be added in time :). A property called "dataset" is exposed by the component containing the form's current state ie.

  • field values
  • errors

the dataset object can be then used to dictate how different html elements will be behaving.

<div class="form-group">
    <input type="text" class="form-control required" id="fieldnameInput" placeholder="" name="fieldname"
           v-model="dataset.fieldname"
           v-bind:class="{ 'is-invalid': dataset.errors.has('fieldname') }">
    <span class="invalid-feedback" role="alert" v-if="dataset.errors.has('fieldname')">
        <strong v-text="dataset.errors.get('fieldname')"></strong>
    </span>
</div>

In the example above

  • the value of the input is set to the dataset equivalent object key
  • a class named "is-invalid" will be added to the input if the dataset's errors object contains the specific field
  • a span will be visible under the same condition, with it's text set to the dataset's specific error

form submission

the form submission is handled by the component which will submit a request with all the form data to the url set by the component's action property and with the method set by the component's method property.

<vue-form method="post"
          action="/the/form/action"
          //...
          >

in the example above a post request will be sent to /the/form/action

response

the component will emit a success or failure event depending on the outcome of the submission request. Both events contain the relevant server response so that further handling can be implemented. The two event handlers have to simply be set on the component.

<vue-form // ...
          v-on:success="onSuccess"
          v-on:failure="onFailure"
          >
let vm = new Vue({

    //...

    methods: {

        onFailure(error) {
            
            // handle an unsuccessful form submission here
            
        },

        onSuccess(response) {
            
            // handle the successful form submission here
            
        }
        
    }

});