Giter Site home page Giter Site logo

vue-vform's Introduction

vue-vform

Vue.js 2 form component that integrates jQuery Validation and Axios.

Install

Yarn

yarn add vue-vform --dev

NPM

npm install vue-vform --save-dev

Prerequisites

Usage

Define vform component markup inside your custom component.

For example in your custom-form-component.vue:

<template>

  <vform
    request

    :params="user"
    method="post"
    action="/api/v1/user/add"
    @validate="mySubmitCallback">

    <!-- Your cool stuff -->
    <div class="form-group">
      <label for="txtname">Name</label>
      <input
        name="txtname"
        v-model="user.name"
        required
        data-msg-required="Enter your name"
        type="text" class="form-control">
    </div>

    <div class="form-group">
      <label for="txtemail">E-mail</label>
      <input
        name="txtemail"
        v-model="user.email"
        required
        data-msg-required="Enter your E-mail"
        data-rule-email="true"
        data-msg-email="Enter a valid E-mail address"
        type="text" class="form-control">
    </div>
    <!-- //Your cool stuff -->

    <button type="submit" class="btn btn-primary">Submit</button>

  </vform>

</template>

<script>
  export default {
    data () {
      return {
      	user: {
          name: '',
          email: ''
        }
      }
    },

    methods: {
      /**
       * Callback method when validation is completed.
       */
      mySubmitCallback (promise) {
        promise
          .then(response => response.data)
          .then(data => console.log(data))
          .catch(err => console.log(err.message))
      }
    }
  }
</script>

In your entry app:

const Vue = require('vue')

// jQuery and jQuery Validation
window.$ = window.jQuery = require('jquery')
require('jquery-validation')

// If you want auto form Ajax request (optional)
window.axios = require('axios')

Vue.component('vform', require('vue-vform'))
Vue.component('custom-form-component', require('./components/custom-form-component'))

const app = new Vue({
  el: '#app'
})

Attributes

method (optional)

The request method (POST, PUT, DELETE, PATCH). For dynamic value use v-bind:method="myMethod" or :method="myMethod".

action (optional)

The request URL.

request (optional)

If request (Boolean) attribute is defined vform performs an Ajax Request using Axios and a Promise object is passed to your callback. Make sure that you have Axios before.

params (optional)

The component data binding (usually FormData or plain object {} values) that it will send if request option was setted. (use v-bind:param="mydata" or :param="mydata" property)

accept (optional)

The request Accept header. Default: application/json

Events

@validate

Event when validation is completed. You need to pass the callback defined in your methods: .... A Promise object will be passed if request attribute was defined.

Tip

Laravel v5.4 users: It's necessary to define the Axios common headers in your app.js file. That's is useful when your use Laravel v5.4 and Passport.

axios.defaults.headers.common = {
  'Accept': 'application/json',
  'X-Requested-With': 'XMLHttpRequest',
  'X-CSRF-TOKEN': Laravel.csrfToken
}

License

MIT license

© 2017 José Luis Quintana

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.