From d88dde18bb78a4144c3c3d113f5dc3eec364239e Mon Sep 17 00:00:00 2001 From: hohoTT <609029365@qq.com> Date: Sun, 16 Aug 2015 20:53:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E6=8E=89=E5=95=86?= =?UTF-8?q?=E4=B8=9A=E7=89=88=E7=9A=84=20formvalidation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/app/admin/announcement/announcement.js | 29 +-- static/src/js/app/admin/group/group.js | 2 +- static/src/js/app/admin/group/groupDetail.js | 75 +++---- .../src/js/app/admin/problem/add_problem.js | 183 ++++++----------- .../src/js/app/admin/problem/edit_problem.js | 186 ++++++------------ static/src/js/app/admin/problem/problem.js | 2 +- template/admin/announcement/announcement.html | 12 +- template/admin/group/group_detail.html | 9 +- template/admin/problem/add_problem.html | 26 ++- template/admin/problem/edit_problem.html | 26 ++- 10 files changed, 214 insertions(+), 336 deletions(-) diff --git a/static/src/js/app/admin/announcement/announcement.js b/static/src/js/app/admin/announcement/announcement.js index 13db292..c4eebf6 100644 --- a/static/src/js/app/admin/announcement/announcement.js +++ b/static/src/js/app/admin/announcement/announcement.js @@ -1,4 +1,4 @@ -require(["jquery", "avalon", "csrfToken", "bsAlert", "editor", "formValidation"], +require(["jquery", "avalon", "csrfToken", "bsAlert", "editor", "validator"], function ($, avalon, csrfTokenHeader, bsAlert, editor) { @@ -122,26 +122,15 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "editor", "formValidation"] } //新建公告表单验证与数据提交 - $("#announcement-form") - .formValidation({ - framework: "bootstrap", - fields: { - title: { - validators: { - notEmpty: { - message: "请填写公告标题" - } - } - } - } - } - ).on('success.form.fv', function (e) { - e.preventDefault(); + + + $('form').validator().on('submit', function (e) { + if (!e.isDefaultPrevented()) { var title = $("#title").val(); var content = createAnnouncementEditor.getValue(); if (content == "") { bsAlert("请填写公告内容"); - return; + return false; } $.ajax({ beforeSend: csrfTokenHeader, @@ -156,11 +145,13 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "editor", "formValidation"] createAnnouncementEditor.setValue(""); getPageData(1); } else { - bs_alert(data.data); + bsAlert(data.data); } } }) - }); + return false; + } + }) }); avalon.scan(); }); \ No newline at end of file diff --git a/static/src/js/app/admin/group/group.js b/static/src/js/app/admin/group/group.js index 4ff6ba4..b231212 100644 --- a/static/src/js/app/admin/group/group.js +++ b/static/src/js/app/admin/group/group.js @@ -1,4 +1,4 @@ -require(["jquery", "avalon", "csrfToken", "bsAlert", "formValidation"], function ($, avalon, csrfTokenHeader, bsAlert) { +require(["jquery", "avalon", "csrfToken", "bsAlert"], function ($, avalon, csrfTokenHeader, bsAlert) { avalon.ready(function () { diff --git a/static/src/js/app/admin/group/groupDetail.js b/static/src/js/app/admin/group/groupDetail.js index 8dd0202..c85ff13 100644 --- a/static/src/js/app/admin/group/groupDetail.js +++ b/static/src/js/app/admin/group/groupDetail.js @@ -1,4 +1,4 @@ -require(["jquery", "avalon", "csrfToken", "bsAlert", "formValidation"], function ($, avalon, csrfTokenHeader, bsAlert) { +require(["jquery", "avalon", "csrfToken", "bsAlert", "validator"], function ($, avalon, csrfTokenHeader, bsAlert) { // avalon:定义模式 group_list @@ -90,58 +90,33 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "formValidation"], function } }); - $("#edit_group_form") - .formValidation({ - framework: "bootstrap", - fields: { - name: { - validators: { - notEmpty: { - message: "请填写小组名" - }, - stringLength: { - max: 20, - message: '小组名长度必须在20位之内' + $('form').validator().on('submit', function (e) { + if (!e.isDefaultPrevented()) { + + var group_id = avalon.vmodels.admin.groupId; + var name = vm.name; + var description = vm.description; + var join_group_setting = vm.checkedSetting; + + $.ajax({ + beforeSend: csrfTokenHeader, + url: "/api/admin/group/", + method: "put", + data: {group_id: group_id, name: name, description: description, + join_group_setting: join_group_setting}, + dataType: "json", + success: function (data) { + if (!data.code) { + bsAlert("修改成功"); + } + else { + bsAlert(data.data); } } - }, - description: { - validators: { - notEmpty: { - message: "请填写描述" - }, - stringLength: { - max: 300, - message: '描述长度必须在300位之内' - } - } - } + }); + return false; } - } - ).on('success.form.fv', function (e) { - e.preventDefault(); - var data = { - group_id: avalon.vmodels.admin.groupId, - name: vm.name, - description: vm.description, - join_group_setting: vm.checkedSetting - }; - $.ajax({ - beforeSend: csrfTokenHeader, - url: "/api/admin/group/", - method: "put", - data: data, - dataType: "json", - success: function (data) { - if (!data.code) { - bsAlert("修改成功"); - } - else { - bsAlert(data.data); - } - } - }) - }); + }) }); }); \ No newline at end of file diff --git a/static/src/js/app/admin/problem/add_problem.js b/static/src/js/app/admin/problem/add_problem.js index 40f74da..6eaf4dd 100644 --- a/static/src/js/app/admin/problem/add_problem.js +++ b/static/src/js/app/admin/problem/add_problem.js @@ -1,135 +1,72 @@ -require(["jquery", "avalon", "editor", "uploader", "bsAlert", "csrfToken", "tagEditor", "formValidation", "jqueryUI"], +require(["jquery", "avalon", "editor", "uploader", "bsAlert", "csrfToken", "tagEditor", "validator", "jqueryUI"], function ($, avalon, editor, uploader, bsAlert, csrfTokenHeader) { avalon.ready(function () { avalon.vmodels.addProblem = null; - $("#add-problem-form") - .formValidation({ - framework: "bootstrap", - fields: { - title: { - validators: { - notEmpty: { - message: "请填写题目名称" - }, - stringLength: { - min: 1, - max: 30, - message: "名称不能超过30个字" - } - } - }, - cpu: { - validators: { - notEmpty: { - message: "请输入时间限制" - }, - integer: { - message: "请输入一个合法的数字" - }, - between: { - inclusive: true, - min: 1, - max: 5000, - message: "只能在1-5000之间" - } - } - }, - memory: { - validators: { - notEmpty: { - message: "请输入内存限制" - }, - integer: { - message: "请输入一个合法的数字" - } - } - }, - difficulty: { - validators: { - notEmpty: { - message: "请输入难度" - }, - integer: { - message: "难度用一个整数表示" - } - } - }, - input_description: { - validators: { - notEmpty: { - message: "请填写输入描述" - } - } - }, - output_description: { - validators: { - notEmpty: { - message: "请填写输出描述" - } + $("#add-problem-form").validator() + .on('submit', function (e) { + if (!e.isDefaultPrevented()){ + if (vm.testCaseId == "") { + bsAlert("你还没有上传测试数据!"); + return false; + } + if (vm.description == "") { + bsAlert("题目描述不能为空!"); + return false; + } + if (vm.samples.length == 0) { + bsAlert("请至少添加一组样例!"); + return false; + } + for (var i = 0; i < vm.samples.length; i++) { + if (vm.samples[i].input == "" || vm.samples[i].output == "") { + bsAlert("样例输入与样例输出不能为空!"); + return false; } } - } - }) - .on("success.form.fv", function (e) { - e.preventDefault(); - if (vm.testCaseId == "") { - bsAlert("你还没有上传测试数据!"); - return; - } - if (vm.description == "") { - bsAlert("题目描述不能为空!"); - return; - } - if (vm.samples.length == 0) { - bsAlert("请至少添加一组样例!"); - return; - } - for (var i = 0; i < vm.samples.length; i++) { - if (vm.samples[i].input == "" || vm.samples[i].output == "") { - bsAlert("样例输入与样例输出不能为空!"); - return; + var tags = $("#tags").tagEditor("getTags")[0].tags; + if (tags.length == 0) { + bsAlert("请至少添加一个标签,这将有利于用户发现你的题目!"); + return false; } - } - if (tags.length == 0) { - bsAlert("请至少添加一个标签,这将有利于用户发现你的题目!"); - return; - } - var ajaxData = { - title: vm.title, - description: vm.description, - time_limit: vm.timeLimit, - memory_limit: vm.memoryLimit, - samples: [], - test_case_id: vm.testCaseId, - hint: vm.hint, - source: vm.source, - tags: $("#tags").tagEditor("getTags")[0].tags, - input_description: vm.inputDescription, - output_description: vm.outputDescription, - difficulty: vm.difficulty - }; + var ajaxData = { + id: avalon.vmodels.admin.problemId, + title: vm.title, + description: vm.description, + time_limit: vm.timeLimit, + memory_limit: vm.memoryLimit, + samples: [], + test_case_id: vm.testCaseId, + hint: vm.hint, + source: vm.source, + visible: vm.visible, + tags: tags, + input_description: vm.inputDescription, + output_description: vm.outputDescription, + difficulty: vm.difficulty + }; - for (var i = 0; i < vm.samples.$model.length; i++) { - ajaxData.samples.push({input: vm.samples.$model[i].input, output: vm.samples.$model[i].output}); - } - - $.ajax({ - beforeSend: csrfTokenHeader, - url: "/api/admin/problem/", - dataType: "json", - data: JSON.stringify(ajaxData), - method: "post", - contentType: "application/json", - success: function (data) { - if (!data.code) { - bsAlert("题目添加成功!"); - } - else { - bsAlert(data.data); - } + for (var i = 0; i < vm.samples.$model.length; i++) { + ajaxData.samples.push({input: vm.samples.$model[i].input, output: vm.samples.$model[i].output}); } - }) + $.ajax({ + beforeSend: csrfTokenHeader, + url: "/api/admin/problem/", + dataType: "json", + data: JSON.stringify(ajaxData), + method: "post", + contentType: "application/json", + success: function (data) { + if (!data.code) { + bsAlert("题目添加成功!"); + } + else { + bsAlert(data.data); + } + } + }); + return false; + } }); var testCaseUploader = uploader("#testCaseFile", "/api/admin/test_case_upload/", function (file, response) { diff --git a/static/src/js/app/admin/problem/edit_problem.js b/static/src/js/app/admin/problem/edit_problem.js index 75ebbae..c64d64c 100644 --- a/static/src/js/app/admin/problem/edit_problem.js +++ b/static/src/js/app/admin/problem/edit_problem.js @@ -1,139 +1,75 @@ -require(["jquery", "avalon", "editor", "uploader", "bsAlert", "csrfToken", "tagEditor", "formValidation", "jqueryUI"], +require(["jquery", "avalon", "editor", "uploader", "bsAlert", "csrfToken", "tagEditor", "validator", "jqueryUI"], function ($, avalon, editor, uploader, bsAlert, csrfTokenHeader) { avalon.ready(function () { avalon.vmodels.editProblem = null; - $("#edit-problem-form") - .formValidation({ - framework: "bootstrap", - fields: { - title: { - validators: { - notEmpty: { - message: "请填写题目名称" - }, - stringLength: { - min: 1, - max: 30, - message: "名称不能超过30个字" - } - } - }, - cpu: { - validators: { - notEmpty: { - message: "请输入时间限制" - }, - integer: { - message: "请输入一个合法的数字" - }, - between: { - inclusive: true, - min: 1, - max: 5000, - message: "只能在1-5000之间" - } - } - }, - memory: { - validators: { - notEmpty: { - message: "请输入内存限制" - }, - integer: { - message: "请输入一个合法的数字" - } - } - }, - difficulty: { - validators: { - notEmpty: { - message: "请输入难度" - }, - integer: { - message: "难度用一个整数表示" - } - } - }, - input_description: { - validators: { - notEmpty: { - message: "请填写输入描述" - } - } - }, - output_description: { - validators: { - notEmpty: { - message: "请填写输出描述" - } + $("#edit-problem-form").validator() + .on('submit', function (e) { + if (!e.isDefaultPrevented()){ + if (vm.testCaseId == "") { + bsAlert("你还没有上传测试数据!"); + return false; + } + if (vm.description == "") { + bsAlert("题目描述不能为空!"); + return false; + } + if (vm.samples.length == 0) { + bsAlert("请至少添加一组样例!"); + return false; + } + for (var i = 0; i < vm.samples.length; i++) { + if (vm.samples[i].input == "" || vm.samples[i].output == "") { + bsAlert("样例输入与样例输出不能为空!"); + return false; } } - } - }) - .on("success.form.fv", function (e) { - e.preventDefault(); - if (vm.testCaseId == "") { - bsAlert("你还没有上传测试数据!"); - return; - } - if (vm.description == "") { - bsAlert("题目描述不能为空!"); - return; - } - if (vm.samples.length == 0) { - bsAlert("请至少添加一组样例!"); - return; - } - for (var i = 0; i < vm.samples.length; i++) { - if (vm.samples[i].input == "" || vm.samples[i].output == "") { - bsAlert("样例输入与样例输出不能为空!"); - return; + var tags = $("#tags").tagEditor("getTags")[0].tags; + if (tags.length == 0) { + bsAlert("请至少添加一个标签,这将有利于用户发现你的题目!"); + return false; } - } - if (tags.length == 0) { - bsAlert("请至少添加一个标签,这将有利于用户发现你的题目!"); - return; - } - var ajaxData = { - id: avalon.vmodels.admin.problemId, - title: vm.title, - description: vm.description, - time_limit: vm.timeLimit, - memory_limit: vm.memoryLimit, - samples: [], - test_case_id: vm.testCaseId, - hint: vm.hint, - source: vm.source, - visible: vm.visible, - tags: $("#tags").tagEditor("getTags")[0].tags, - input_description: vm.inputDescription, - output_description: vm.outputDescription, - difficulty: vm.difficulty - }; + var ajaxData = { + id: avalon.vmodels.admin.problemId, + title: vm.title, + description: vm.description, + time_limit: vm.timeLimit, + memory_limit: vm.memoryLimit, + samples: [], + test_case_id: vm.testCaseId, + hint: vm.hint, + source: vm.source, + visible: vm.visible, + tags: tags, + input_description: vm.inputDescription, + output_description: vm.outputDescription, + difficulty: vm.difficulty + }; - for (var i = 0; i < vm.samples.$model.length; i++) { - ajaxData.samples.push({input: vm.samples.$model[i].input, output: vm.samples.$model[i].output}); - } - - $.ajax({ - beforeSend: csrfTokenHeader, - url: "/api/admin/problem/", - dataType: "json", - data: JSON.stringify(ajaxData), - method: "put", - contentType: "application/json", - success: function (data) { - if (!data.code) { - bsAlert("题目编辑成功!"); - } - else { - bsAlert(data.data); - } + for (var i = 0; i < vm.samples.$model.length; i++) { + ajaxData.samples.push({input: vm.samples.$model[i].input, output: vm.samples.$model[i].output}); } - }) + $.ajax({ + beforeSend: csrfTokenHeader, + url: "/api/admin/problem/", + dataType: "json", + data: JSON.stringify(ajaxData), + method: "put", + contentType: "application/json", + success: function (data) { + if (!data.code) { + bsAlert("题目编辑成功!"); + } + else { + bsAlert(data.data); + } + } + + }); + return false; + } }); var vm = avalon.define({ diff --git a/static/src/js/app/admin/problem/problem.js b/static/src/js/app/admin/problem/problem.js index 4f87b3d..65c85dc 100644 --- a/static/src/js/app/admin/problem/problem.js +++ b/static/src/js/app/admin/problem/problem.js @@ -1,4 +1,4 @@ -require(["jquery", "avalon", "csrfToken", "bsAlert", "formValidation"], function ($, avalon, csrfTokenHeader, bsAlert) { +require(["jquery", "avalon", "csrfToken", "bsAlert"], function ($, avalon, csrfTokenHeader, bsAlert) { avalon.ready(function () { avalon.vmodels.problemList = null; diff --git a/template/admin/announcement/announcement.html b/template/admin/announcement/announcement.html index 021cf29..03f110f 100644 --- a/template/admin/announcement/announcement.html +++ b/template/admin/announcement/announcement.html @@ -53,11 +53,17 @@

添加公告

-
-
+
+ + +
+
- + +
diff --git a/template/admin/group/group_detail.html b/template/admin/group/group_detail.html index 79cb144..525d7bc 100644 --- a/template/admin/group/group_detail.html +++ b/template/admin/group/group_detail.html @@ -30,12 +30,17 @@
- + +
- + +
diff --git a/template/admin/problem/add_problem.html b/template/admin/problem/add_problem.html index 67e79cf..2efb39d 100644 --- a/template/admin/problem/add_problem.html +++ b/template/admin/problem/add_problem.html @@ -3,7 +3,9 @@
- + +
@@ -15,17 +17,23 @@
- + +
- + +
- + +
@@ -41,12 +49,18 @@

+ ms-duplex="inputDescription"data-error="请填写输入描述" + maxlength="10000" required> + +

+ ms-duplex="outputDescription"data-error="请填写输出描述" + maxlength="10000" required> + +

diff --git a/template/admin/problem/edit_problem.html b/template/admin/problem/edit_problem.html index 0840d97..02ec147 100644 --- a/template/admin/problem/edit_problem.html +++ b/template/admin/problem/edit_problem.html @@ -3,7 +3,9 @@
- + +
@@ -15,17 +17,23 @@
- + +
- + +
- + +
@@ -41,12 +49,18 @@

+ ms-duplex="inputDescription" data-error="请填写输入描述" + maxlength="10000" required> + +

+ ms-duplex="outputDescription" data-error="请填写输出描述" + maxlength="10000" required> + +

From 164e44b3982d3125635ce32ee5ae1a612e311a6b Mon Sep 17 00:00:00 2001 From: hohoTT <609029365@qq.com> Date: Mon, 17 Aug 2015 14:51:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E6=8E=89=E5=95=86=E4=B8=9A=E7=89=88=E7=9A=84?= =?UTF-8?q?=20formvalidation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/src/js/app/admin/user/userList.js | 97 +++++++----------------- template/admin/problem/edit_problem.html | 2 +- template/admin/user/user_list.html | 15 +++- 3 files changed, 40 insertions(+), 74 deletions(-) diff --git a/static/src/js/app/admin/user/userList.js b/static/src/js/app/admin/user/userList.js index 277aa88..0842dc5 100644 --- a/static/src/js/app/admin/user/userList.js +++ b/static/src/js/app/admin/user/userList.js @@ -1,4 +1,4 @@ -require(["jquery", "avalon", "csrfToken", "bsAlert", "formValidation"], function ($, avalon, csrfTokenHeader, bsAlert) { +require(["jquery", "avalon", "csrfToken", "bsAlert", "validator"], function ($, avalon, csrfTokenHeader, bsAlert) { // avalon:定义模式 userList @@ -87,77 +87,36 @@ require(["jquery", "avalon", "csrfToken", "bsAlert", "formValidation"], function }); } - $("#edit_user-form") - .formValidation({ - framework: "bootstrap", - fields: { - username: { - validators: { - notEmpty: { - message: "请填写用户名" - }, - stringLength: { - min: 3, - max: 30, - message: '用户名长度必须在3到30位之间' + $("#edit_user-form").validator() + .on('submit', function (e) { + if (!e.isDefaultPrevented()) { + var data = { + username: vm.username, + real_name: vm.realName, + email: vm.email, + id: vm.id, + admin_type: vm.adminType + }; + if ($("#password").val() !== "") + data.password = $("#password").val(); + $.ajax({ + beforeSend: csrfTokenHeader, + url: "/api/admin/user/", + data: data, + dataType: "json", + method: "put", + success: function (data) { + if (!data.code) { + bsAlert("编辑成功!"); + getPageData(1); + $("#password").val(""); + } else { + bsAlert(data.data); } } - }, - real_name: { - validators: { - notEmpty: { - message: "请填写真实姓名" - } - } - }, - email: { - validators: { - notEmpty: { - message: "请填写电子邮箱邮箱地址" - }, - emailAddress: { - message: "请填写有效的邮箱地址" - } - } - }, - password: { - validators: { - stringLength: { - min: 6, - max: 30, - message: '密码长度必须在6到30位之间' - } - } - } + }); + return false; } - } - ).on('success.form.fv', function (e) { - e.preventDefault(); - var data = { - username: vm.username, - real_name: vm.realName, - email: vm.email, - id: vm.id, - admin_type: vm.adminType - }; - if ($("#password").val() !== "") - data.password = $("#password").val(); - $.ajax({ - beforeSend: csrfTokenHeader, - url: "/api/admin/user/", - data: data, - dataType: "json", - method: "put", - success: function (data) { - if (!data.code) { - bsAlert("提交成功!"); - getPageData(1); - $("#password").val(""); - } else { - bsAlert(data.data); - } - } - }) }); }); diff --git a/template/admin/problem/edit_problem.html b/template/admin/problem/edit_problem.html index 02ec147..2b63b2c 100644 --- a/template/admin/problem/edit_problem.html +++ b/template/admin/problem/edit_problem.html @@ -17,7 +17,7 @@
-
diff --git a/template/admin/user/user_list.html b/template/admin/user/user_list.html index 6045bdb..f884efc 100644 --- a/template/admin/user/user_list.html +++ b/template/admin/user/user_list.html @@ -52,19 +52,26 @@
- + +
- + +
+ placeholder="此项留空则保留原密码" data-minlength="6" data-minlength-error="密码不得少于6位"> +
- + +
+ data-error="请填写公告标题(标题不得超过50字)" maxlength="50" required>
diff --git a/template/admin/group/group_detail.html b/template/admin/group/group_detail.html index 525d7bc..208aa71 100644 --- a/template/admin/group/group_detail.html +++ b/template/admin/group/group_detail.html @@ -31,7 +31,7 @@
+ data-error="请填写小组名(名称不能超过20字)" maxlength="20" required>
diff --git a/template/admin/problem/add_problem.html b/template/admin/problem/add_problem.html index 04d126b..fadea4c 100644 --- a/template/admin/problem/add_problem.html +++ b/template/admin/problem/add_problem.html @@ -4,7 +4,7 @@
+ data-error="请填写题目名称(名称不能超过50个字)" maxlength="50" required>
@@ -18,7 +18,7 @@
+ data-error="请输入时间限制(保证是一个1000-5000的合法整数)" required>
diff --git a/template/admin/problem/edit_problem.html b/template/admin/problem/edit_problem.html index 2b63b2c..cdee4a0 100644 --- a/template/admin/problem/edit_problem.html +++ b/template/admin/problem/edit_problem.html @@ -18,7 +18,7 @@
+ data-error="请输入时间限制(保证是一个1000-5000的合法整数)" required>
diff --git a/template/admin/user/user_list.html b/template/admin/user/user_list.html index f884efc..8e8c0f4 100644 --- a/template/admin/user/user_list.html +++ b/template/admin/user/user_list.html @@ -70,7 +70,7 @@
+ data-error="请填写邮箱地址(并保证是正确的邮箱格式)" required>