浏览代码

1.替换bs4.0正式版
2.修改验证模板

olym 7 年之前
父节点
当前提交
15a6b1de25

+ 9 - 4
app.js

@@ -18,9 +18,13 @@ module.exports = app => {
     app.BaseController = BaseController;
 
     // 自定义手机校验规则
-    app.validator.addRule('isMobile', (rule, value) => {
+    app.validator.addRule('mobile', (rule, value) => {
         try {
             const regPhone = /^1([34578]\d)\d{8}$/;
+            const allowEmpty = rule.allowEmpty === true;
+            if (allowEmpty && value === '') {
+                return true;
+            }
             if (!(value.length === 11 && regPhone.test(value))) {
                 throw 'please enter the correct phone number';
             }
@@ -30,10 +34,11 @@ module.exports = app => {
     });
 
     // 自定义手机IP规则
-    app.validator.addRule('isIP', (rule, value) => {
+    app.validator.addRule('ip', (rule, value) => {
         try {
-            if (value === '') {
-                throw 'please enter the correct ip address';
+            const allowEmpty = rule.allowEmpty === true;
+            if (allowEmpty && value === '') {
+                return true;
             }
             const regIP = /^(\d{2,})\.(\d+)\.(\d+)\.(\d+)$/;
             if (!regIP.test(value)) {

+ 7 - 2
app/extend/helper.js

@@ -56,8 +56,13 @@ module.exports = {
             }
 
             // 自定义判断
-            if (rule[index] === 'isMobile' || rule[index] === 'isIP') {
-                result[index] = rule[index];
+            const customType = ['mobile', 'ip'];
+            // 自定义且带参数
+            if (customType.indexOf(type) >= 0) {
+                result[index][type] = true;
+                if (rule[index].allowEmpty !== undefined) {
+                    result[index].required = !rule[index].allowEmpty;
+                }
             }
 
         }

文件差异内容过多而无法显示
+ 4 - 4
app/public/css/bootstrap/bootstrap.min.css


文件差异内容过多而无法显示
+ 6 - 5
app/public/js/bootstrap/bootstrap.min.js


+ 2 - 2
app/public/js/validate.extend.js

@@ -7,13 +7,13 @@
  */
 
 //检测手机号是否正确
-jQuery.validator.addMethod("isMobile", function (value, element) {
+jQuery.validator.addMethod("mobile", function (value, element) {
     let length = value.length;
     let regPhone = /^1([34578]\d)\d{8}$/;
     return this.optional(element) || (length == 11 && regPhone.test(value));
 }, "请正确填写您的手机号码");
 
-jQuery.validator.addMethod("isIP", function (value, element) {
+jQuery.validator.addMethod("ip", function (value, element) {
     if (value === '') {
         return false;
     }

+ 4 - 4
test/app/extend/helper.test.js

@@ -19,8 +19,8 @@ describe('test/app/extend/helper.test.js', () => {
             group_id: { type: 'integer', required: true, allowEmpty: false, min: 4, max: 10 },
             password: { type: 'password', required: true, allowEmpty: false, min: 4 },
             confirm_password: { type: 'password', required: true, allowEmpty: false, min: 4, compare: 'password' },
-            ip: 'isIP',
-            telephone: 'isMobile',
+            ip: { type: 'ip', allowEmpty: false },
+            telephone: { type: 'mobile', allowEmpty: false },
         };
         // 转换为json验证
         let convertRule = ctx.helper.validateConvert(rule);
@@ -30,8 +30,8 @@ describe('test/app/extend/helper.test.js', () => {
             group_id: { required: true, min: 4, max: 10 },
             password: { required: true, minlength: 4 },
             confirm_password: { required: true, minlength: 4, equalTo: '#password' },
-            ip: 'isIP',
-            telephone: 'isMobile',
+            ip: { type: 'ip', allowEmpty: false },
+            telephone: { type: 'mobile', allowEmpty: false },
         };
         expectRule = JSON.stringify(expectRule);
         assert(convertRule === expectRule);