GithubHelp home page GithubHelp logo

vue_form's Introduction

vue_form 一款基于vue 的form 表单校验插件

引入组件并注册使用

# 用法一
import Form from './form'
import FormItem from './form-item'

Vue.use(Form)
Vue.use(FormItem)

# 用法二
import Form from './form'
import FormItem from './form-item'

Vue.component('lm-form', Form);
Vue.component('lm-form-item', FormItem);

新增校验方法

function checkPhone(value, param){
  var length = value.length;
  var mobile = /(?:0|86|\+86)?1[3456789]\d{9}/;
  return this.optional(value) || (length == 11 && mobile.test(value));  
}

# 方法一、
Vue.use(Form, {
  "phone":{
    "method": checkPhone,
    "message":"请填写正确的电话号码"
  }
})
Vue.use(FormItem)

# 方法二、
Form.addMethod({
  "phone":{
    "method": checkPhone,
    "message":"请填写正确的电话号码"
  }, 
})
Vue.component('l-form', Form);
Vue.component('l-form-item', FormItem);

template中用法

<lm-form ref="form" :model="formModel" :rules="rules" :messages="messages">
  <lm-form-item prop="names" :title="title">
  	<input type="text" name="names" id="names">
  </lm-form-item>
  <lm-form-item prop="phone" title="keyTitle" :rules="demoRules">
    <input type="text" v-model="formModel.phone" name="phone" id="phone">
  </lm-form-item>
  <button type="button" @click="submit()">提交</button>
</lm-form>

js 调用校验

export default {
  data() {
    return {
      title: '标题',
      keyTitle: '关键'
      demoRules: {
        rules: {
          required: true,
          phone: true
        },
        messages: {
          required: '请输入key',
          phone: '请输入正确的手机号码'
        }
      },
      formModel: {
        names: '',
        key: '' 
      },
      rules: {
        names: {
          required: true
        }
      },
      messages: {
        names: {
          required: '请输入用户名'  
        },
        
      }
    } 
  },
  methods: {
    /* 使用promise方式 */
    submit() {
      this.$refs.form.validate().then((form) => {
        console.log(form)
      }).catch(err => {
        console.log(err, 'assets/')
      })
    },
    /* 使用回调函数的方式 */
    submit2() {
      this.$refs.form.validate((form, errors) => {
        console.log(form, errors)
      })
    }
  }
}

vue_form's People

Contributors

qiuyaxian avatar

Watchers

James Cloos avatar

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.