form.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. define(['jquery'], function($) {
  2. var form = {};
  3. form.init = function() {
  4. var modal_form = $('form.form-modal');
  5. var form_modal;
  6. if (modal_form.length > 0) {
  7. var form_modal = modal_form.parents('.modal');
  8. form_modal.on("hidden", function() {
  9. modal_form.resetForm()
  10. })
  11. }
  12. var form_validate = $('form.form-validate');
  13. if (form_validate.length > 0) {
  14. var validate_rule = {
  15. errorElement: 'span',
  16. errorClass: 'help-block-validate',
  17. focusInvalid: true,
  18. highlight: function(element) {
  19. var parent = $(element).data('parent') || '';
  20. if (parent) {
  21. $(parent).addClass('has-error')
  22. } else {
  23. $(element).closest('.form-group').addClass('has-error')
  24. }
  25. },
  26. onkeyup: function(element) {
  27. $(element).valid()
  28. },
  29. onfocusout: function(element) {
  30. $(element).valid()
  31. },
  32. success: function(element) {
  33. var parent = $(element).data('parent') || '';
  34. if (parent) {
  35. $(parent).removeClass('has-error')
  36. } else {
  37. $(element).closest('.form-group').removeClass('has-error')
  38. }
  39. },
  40. errorPlacement: function(error, element) {
  41. var group = element.parents(".input-group");
  42. group.length > 0 ? group.after(error) : element.after(error)
  43. },
  44. submitHandler: function(form) {
  45. var cansubmit = true;
  46. if ($(".form-editor-group").length > 0) {
  47. $(".form-editor-group").each(function() {
  48. var input = $(this).find(".form-editor-input");
  49. if (input.attr('data-rule-required') && $.trim(input.val()) == '') {
  50. $(this).find(".form-editor-btn").trigger('click');
  51. input.focus().blur().focus();
  52. cansubmit = false;
  53. return false
  54. }
  55. })
  56. }
  57. if (!cansubmit) {
  58. return
  59. }
  60. var submit_button = $("input[type='submit']", form);
  61. var buttontype = 'input';
  62. var html = submit_button.val();
  63. if (submit_button.length <= 0) {
  64. submit_button = $("button[type='submit']", form);
  65. buttontype = 'button';
  66. html = submit_button.html()
  67. }
  68. if ($(form).attr('stop') == '1') {
  69. return
  70. }
  71. var confirm = submit_button.data('confirm') || submit_button.data('confirm');
  72. var handler = function() {
  73. if (buttontype == 'button') {
  74. submit_button.html('<i class="fa fa-spinner fa-spin"></i> ' + tip.lang.processing)
  75. } else {
  76. submit_button.val(tip.lang.processing)
  77. }
  78. var timeout = 1000 * 3600;
  79. submit_button.attr('disabled', true);
  80. $(form).ajaxSubmit({
  81. timeout: timeout,
  82. dataType: "json",
  83. success: function(a) {
  84. if (a.result.url) {
  85. a.result.url = a.result.url.replace(/&amp;/ig, "&");
  86. a.result.url = a.result.url.replace('¬', "&not")
  87. }
  88. if (a.status != 1) {
  89. submit_button.removeAttr('disabled'), buttontype == 'button' ? submit_button.html(html) : submit_button.val(html);
  90. form_modal && form_modal.modal("hide"), tip.msgbox.err(a.result.message || a.result || tip.lang.error, a.result.url)
  91. } else {
  92. tip.msgbox.suc(a.result.message || tip.lang.success, a.result.url)
  93. }
  94. },
  95. error: function(a) {
  96. submit_button.removeAttr('disabled'), buttontype == 'button' ? submit_button.html(html) : submit_button.val(html), form_modal && form_modal.modal("hide");
  97. tip.msgbox.err(tip.lang.error)
  98. }
  99. });
  100. return false
  101. };
  102. if (confirm) {
  103. tip.confirm(confirm, handler)
  104. } else {
  105. handler()
  106. }
  107. }
  108. };
  109. myrequire(['jquery.form', 'jquery.validate'], function() {
  110. var cnmsg = {
  111. required: "此项必须填写",
  112. remote: "请修正该字段",
  113. email: "请输入正确格式的电子邮件",
  114. url: "请输入正确的网址",
  115. date: "请输入正确的日期",
  116. dateISO: "请输入合法的日期 (ISO).",
  117. number: "请输入数字格式",
  118. digits: "请输入整数格式",
  119. creditcard: "请输入合法的信用卡号",
  120. equalTo: "请再次输入相同的值",
  121. accept: "请输入拥有合法后缀名的字符串",
  122. maxlength: $.validator.format("请输入一个长度最多是 {0} 的字符串"),
  123. minlength: $.validator.format("请输入一个长度最少是 {0} 的字符串"),
  124. rangelength: $.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
  125. range: $.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
  126. max: $.validator.format("请输入一个最大为 {0} 的值"),
  127. min: $.validator.format("请输入一个最小为 {0} 的值")
  128. };
  129. $.extend($.validator.messages, cnmsg);
  130. $.validator.addMethod("chinese", function(value, element) {
  131. var chinese = /^[一-龥]+$/;
  132. return this.optional(element) || (chinese.test(value))
  133. }, "只能输入中文"), $.validator.methods.url = function(value, element) {
  134. return this.optional(element) || /^((http|https|ftp):\/\/)?(\w(\:\w)?@)?([0-9a-z_-]+\.)*?([a-z]{2,6}(\.[a-z]{2})?(\:[0-9]{2,6})?)((\/[^?#<>\/\\*":]*)+(\?[^#]*)?(#.*)?)?$/i.test(value)
  135. };
  136. form_validate.each(function() {
  137. var form = jQuery(this);
  138. form.validate(validate_rule)
  139. });
  140. $('#page-loading').remove()
  141. })
  142. }
  143. };
  144. return form
  145. })