11 Star 17 Fork 3

/formModel.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
form-model.min.js 4.74 KB
一键复制 编辑 原始数据 按行查看 历史
提交于 2016-07-14 14:50 . 2016/07/14
/**
* 支持自动映射、自动验证和自动完成的表单模型类
* Author: 陆楚良
* Version: 1.2.1
* Date: 2016/07/14
* QQ: 874449204
*
* https://git.oschina.net/luchg/formModel.js
*
* License: http://opensource.org/licenses/MIT
*/
!function(){function t(t){"use strict";function e(t,e){e=e||2;for(var n=0,a=0;a<t.length;a++)n+=t.charCodeAt(a)>=0&&t.charCodeAt(a)<=255?1:e;return n}function n(e){var n=t(e).serializeArray(),a={},i={};return t.each(n,function(t,e){if(0!=e.name.indexOf("[")){var n=e.name,r=e.name.match(/^([^\[]+)\[(.*)\]/);if(r){n=r[1];var c=r[2];""===c&&(i.hasOwnProperty(n)||(i[n]=-1),c=i[n]+=1),a.hasOwnProperty(n)&&s(a[n])||(a[n]={}),a[n][c]=e.value}else a[n]=e.value}}),a}function a(){this.$data={},this.setMap(this.$m()),this.setVali(this.$v()),this.setAuto(this.$a());var t=this.initialize;t.apply(this,[].slice.call(arguments))}function i(){var e={},n=this.$m;return t.each(this.$data,function(t,a){n.hasOwnProperty(t)&&(t=n[t]),e[t]=a}),e}function r(){for(var n,a,i,r,c=this.$v,o={},s=[],h=0;h<this.$v.length;h++)if(n=null,a=c[h].name,!this.$patch||!o.hasOwnProperty(a)){if(this.$data.hasOwnProperty(a)){if(i=this.$data[a],r=0!==c[h].type.indexOf("!"),c[h].condition==this.constructor.VALUE_VALIDATE&&""===i)continue;switch(c[h].type.replace(/^\!/,"").toLowerCase()){case"regexp":var f="string"==t.type(c[h].action)?l[c[h].action]:c[h].action;f.test(i)!=r&&(n={name:a,msg:c[h].error});break;case"callback":c[h].action.call(this,i,a)!=r&&(n={name:a,msg:c[h].error});break;case"confirm":this.$data[c[h].action]===i!=r&&(n={name:a,msg:c[h].error});break;case"equal":c[h].action===i!=r&&(n={name:a,msg:c[h].error});break;case"in":t.inArray(i,c[h].action)>-1!=r&&(n={name:a,msg:c[h].error});break;case"length":var f=i.length,m=u(c[h].action)?[c[h].action,c[h].action]:c[h].action;((null===m[0]||f>=m[0])&&(null===m[1]||f<=m[1]))!=r&&(n={name:a,msg:c[h].error});break;case"lengthat":var f=e(i,c[h].extra||2),m=u(c[h].action)?[c[h].action,c[h].action]:c[h].action;((null===m[0]||f>=m[0])&&(null===m[1]||f<=m[1]))!=r&&(n={name:a,msg:c[h].error});break;case"between":var f=Number(i);(f>=c[h].action[0]&&f<=c[h].action[1])!=r&&(n={name:a,msg:c[h].error})}}else c[h].condition===this.constructor.MUST_VALIDATE&&(n={name:a,msg:c[h].error});if(n){if(n.msg="function"==t.type(n.msg)?n.msg.call(this,a,i):n.msg,!this.$patch)return n;o[n.name]=!0,s.push(n)}}return s.length>0?s:void 0}function c(){var e=this,n=this.$data;return t.each(this.$a,function(t,a){switch(a.type.toLowerCase()){case"field":n[a.name]=n[a.action];break;case"ignore":""===n[a.name]&&delete n[a.name];break;case"default":case"string":case"callback":default:n[a.name]=h(a.action)?a.action.call(e,n.hasOwnProperty(a.name)?n[a.name]:o,a.name):a.action}}),n}var o=void 0,s=function(e){return"object"==t.type(e)},u=function(e){return"number"==t.type(e)},h=function(e){return"function"==t.type(e)},l={require:/\S+/,email:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,url:/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/,currency:/^(0|[1-9][0-9]*)(\.[0-9]{1,2})?$/,number:/^\d+$/,zip:/^\d{6}$/,integer:/^(0|-?[1-9][0-9]*)$/,pinteger:/^(0|[1-9][0-9]*)$/,"double":/^-?(0|[1-9][0-9]*)\.[0-9]+$/,pdouble:/^(0|[1-9][0-9]*)\.[0-9]+$/,english:/^[A-Za-z]+$/,chinese:/^[\u4e00-\u9fa5]+$/};return a.extend=function(e){var n=function(){a.apply(this,[].slice.call(arguments))};return n.extend=this.extend,n.parent=this,n.create=this.create,n.EXISTS_VALIDATE=this.EXISTS_VALIDATE,n.MUST_VALIDATE=this.MUST_VALIDATE,n.VALUE_VALIDATE=this.VALUE_VALIDATE,n.serializeObject=this.serializeObject,t.extend(n.prototype,this.prototype,e),n},a.create=function(t){return new(this.extend(t))},a.EXISTS_VALIDATE=0,a.MUST_VALIDATE=1,a.VALUE_VALIDATE=2,a.serializeObject=n,a.prototype={initialize:function(){},$patch:!1,$m:function(){return{}},$v:function(){return[]},$a:function(){return[]},check:function(t,e){this.$data=t,this.$data=i.call(this);var n=r.call(this);n?e.call(this,n):(this.$data=c.call(this),e.call(this,null,this.$data))},setMap:function(t){this.$m=t},setVali:function(e){var n=this;this.$v=[],t.each(e,function(e,i){var r={};s(i)?r=i:(i[0]!==o&&(r.name=i[0]),i[1]!==o&&(r.action=i[1]),i[2]!==o&&(r.error=i[2]),i[3]!==o&&(r.type=i[3]),i[4]!==o&&(r.condition=i[4]),i[5]!==o&&(r.extra=i[5])),n.$v.push(t.extend({name:"",action:o,error:"",type:"regexp",condition:a.EXISTS_VALIDATE,extra:o},r))})},setAuto:function(e){var n=this;this.$a=[],s(e)?t.each(e,function(t,e){n.$a.push({name:t,action:e,type:"default"})}):t.each(e,function(e,a){if(!s(a)){var i={};s(a)?i=a:(a[0]!==o&&(i.name=a[0]),a[1]!==o&&(i.action=a[1]),a[2]!==o&&(i.type=a[2])),n.$a.push(t.extend({name:"",action:o,type:"default"},i))}})}},a}"function"==typeof define?define(function(){return t}):"undefined"!=typeof exports?module.exports=t:window.formModel=t($)}();
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/luchg/formModel.js.git
git@gitee.com:luchg/formModel.js.git
luchg
formModel.js
formModel.js
master

搜索帮助