123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- (function ($) {
- //修改MVC Model验证为EasyUI模式
- $.fn.makeValidationEasyUI = function (options) {
- var settings = {
- showTriangle: true,
- promptPosition: "topRight"
- };
- $.extend(settings, options);
- //把验证标记切换到easyUI生成的input
- // $("[comboname]").each(function (i) {
- // var $this = $(this);
- // var $easyUI = $("[name='" + $this.attr("comboname") + "']");
- // if ($this.attr("data-val")) {
- // for (var j = this.attributes.length - 1; j >= 0; j--) {
- // if (this.attributes[j].name.indexOf("data-val") >= 0) {
- // $easyUI.attr(this.attributes[j].name, this.attributes[j].value);
- // $this.removeAttr(this.attributes[j].name);
- // }
- // }
- // $this.removeAttr("data-val");
- // }
- // });
- return this.each(function () {
- var $form = $(this);
- //#region message prompt functions
- /*
- * Inline Form Validation Engine 1.3.9.5, jQuery plugin
- *
- * Copyright(c) 2009, Cedric Dugas
- * http://www.position-relative.net
- *
- * Form validation engine which allow custom regex rules to be added.
- * Licenced under the MIT Licence
- * Modified by Jeffrey Lee, http://blog.darkthread.net, to support ASP.NET MVC 3
- * Modified by wufengshuo to EasyUIValidate type
- */
- //新增提示对象
- function showtip(event) {//input
- movevalidate_tip($(event.currentTarget), event.data.name);
- $("div[data-valeasyui-for='" + event.data.name + "']").show();
- }
- function hidetip(event) {//input
- $("div[data-valeasyui-for='" + event.data.name + "']").hide();
- }
- function addvalidate_tip(caller, promptText, type, ajaxed) {
- var bowintip = $("div[data-valeasyui-for='" + caller.name + "']");
- if (bowintip.length == 0) {
- bowintip = $("<div class=\"validatebox-tip\">" + "<span class=\"validatebox-tip-content\">" + "</span>" + "<span class=\"validatebox-tip-pointer\">" + "</span>" + "</div>").appendTo("body");
- bowintip.attr("data-valeasyui-for", caller.name);
- }
- bowintip.find(".validatebox-tip-content").html(promptText);
- return bowintip;
- }
- function movevalidate_tip($caller, name) {
- var bowintip = $("div[data-valeasyui-for='" + name + "']");
- if (bowintip) {
- var tip_pointer = bowintip.find(".validatebox-tip-pointer");
- var tip_content = bowintip.find(".validatebox-tip-content");
- bowintip.show();
- bowintip.css("top", $caller.offset().top - (tip_content._outerHeight() - $caller._outerHeight()) / 2);
- // if (_3ae.options.tipPosition == "left") {
- //bowintip.css("left", box.offset().left - bowintip._outerWidth());
- //bowintip.addClass("validatebox-tip-left");
- // } else {
- bowintip.css("left", $caller.offset().left + $caller._outerWidth());
- bowintip.removeClass("validatebox-tip-left");
- // }
- tip_pointer.css("top", (tip_content._outerHeight() - tip_pointer._outerHeight()) / 2);
- bowintip.hide();
- }
- };
- function removevalidate_tip(caller) {
- var $caller = findShowTipInput(caller);
- var bowintip = $("div[data-valeasyui-for='" + caller.name + "']");
- if (bowintip) {
- $caller.removeClass("ValidateEntity-invalid");
- $caller.removeClass("input-validation-error");
- $caller.off("mouseenter", showtip);
- $caller.off("mouseleave", hidetip);
- bowintip.remove();
- }
- };
- function onEasyUIChange(newvalue, oldvalue) {
- }
- function showInvalidPrompt(caller, message, error) {
- var $caller = findShowTipInput(caller);
- //function () { showtip(caller); }, function () { hidetip(caller); }
- $caller.on("mouseenter", caller, showtip);
- $caller.on("mouseleave", caller, hidetip);
- $caller.addClass("ValidateEntity-invalid");
- if ($caller.parent().hasClass("easyui-combogridX")) {
- $caller.addClass("input-validation-error");
- var sourceobj = $("[comboname='" + caller.name + "']");
- sourceobj.combogridX({ onChange: function () {
- var validator = $form.validate();
- var vitem = sourceobj.find(".combogridX-value")[0];
- if (vitem.name in validator.submitted || !validator.optional(vitem)) {
- if (validator.element(vitem)) {
- $caller.removeClass("ValidateEntity-invalid");
- $caller.removeClass("input-validation-error");
- $caller.off("mouseenter", showtip);
- $caller.off("mouseleave", hidetip);
- }
- else {
- $caller.on("mouseenter", caller, showtip);
- $caller.on("mouseleave", caller, hidetip);
- $caller.addClass("ValidateEntity-invalid");
- $caller.addClass("input-validation-error");
- }
- }
- }
- });
- }
- if ($caller.is(".combo-text") && !$caller.parent().hasClass("easyui-combogridX")) {
- $caller.addClass("input-validation-error");
- var sourceobj = $("[comboname='" + caller.name + "']");
- sourceobj.combo({ onChange: function (newvalue, oldvalue) {
- var $this = $(this);
- $this.val(newvalue);
- //$this.attr("name", $this.attr("comboname"));
- var vitem = $("[name='" + $this.attr("comboname") + "']")[0];
- var validator = $form.validate();
- if (vitem.name in validator.submitted || !validator.optional(vitem)) {
- if (validator.element(vitem)) {
- $caller.removeClass("ValidateEntity-invalid");
- $caller.removeClass("input-validation-error");
- $caller.off("mouseenter", showtip);
- $caller.off("mouseleave", hidetip);
- }
- else {
- $caller.on("mouseenter", caller, showtip);
- $caller.on("mouseleave", caller, hidetip);
- $caller.addClass("ValidateEntity-invalid");
- $caller.addClass("input-validation-error");
- }
- }
- //$this.attr("name", "");
- }
- });
- //$caller.focus();
- }
- var validate_tip = addvalidate_tip(caller, message, "error", false);
- movevalidate_tip($caller, caller.name);
- var validator = $form.validate();
- // if (validator.errorList[validator.errorList.length - 1].element.name == caller.name) {
- // for (var i = validator.errorList.length - 1; i >= 0; i--) {
- // if (validator.errorList[i].element.type != "hidden") {
- // validator.errorList[i].element.focus();
- // break;
- // }
- // }
- // }
- return validate_tip;
- }
- function hideInvalidPrompt(caller) {
- removevalidate_tip(caller);
- }
- //#endregion
- function findShowTipInput(caller) {
- var $caller = $(caller);
- var $ShowTipcaller;
- if ($caller.is(".combo-value"))
- $ShowTipcaller = $caller.parent().find(".combo-text");
- else if ($caller.is(".combogridX-value"))
- $ShowTipcaller = $caller.parent().find(".combo-text");
- else
- $ShowTipcaller = $caller;
- return $ShowTipcaller;
- }
- function custErrorPlacement(error, inputElement) { // 'this' is the form element
- var msg = error.text();
- if (msg.length > 1 && !inputElement.hasClass("datebox"))
- error.data("inv_msg_prompt", showInvalidPrompt(inputElement[0], msg, error));
- else
- hideInvalidPrompt(inputElement[0]);
- }
- function custSuccess(error) { // 'this' is the form element
- var p = error.data("inv_msg_prompt");
- if (p) p.remove();
- }
- //#region change errorPlacement
- var valdSettings = $form.data("validator").settings;
- // $form.find("input[name=" + value.name + "]").each(function (index, value) {
- // if ($form.data("validator").settings.rules[value.name]) {
- //
- // }
- // });
- valdSettings.errorElement = "div";
- valdSettings.errorClass = "input-validation-error ValidateEntity-invalid";
- valdSettings.errorPlacement = $.proxy(custErrorPlacement, this);
- valdSettings.success = $.proxy(custSuccess, this);
- valdSettings.ignore = "";
- $form.validate(valdSettings);
- //#endregion
- });
- }
- })(jQuery);
- //----------------------------------------------
- // Begin MVC Validate Extend
- //-----------------------------------------------
- (function () {
- var $jQval = $.validator;
- var adapters = $.validator.unobtrusive.adapters;
- function getModelPrefix(fieldName) {
- return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
- }
- function appendModelPrefix(value, prefix) {
- if (value.indexOf("*.") === 0) {
- value = value.replace("*.", prefix);
- }
- return value;
- }
- function setValidationValues(options, ruleName, value) {
- options.rules[ruleName] = value;
- if (options.message) {
- options.messages[ruleName] = options.message;
- }
- }
- $jQval.addMethod('notlessthan', function (value, element, otherproperty) {
- if (value) {
- try {
- var prefix = getModelPrefix(element.name);
- var fullOtherName = appendModelPrefix(otherproperty, prefix);
- var other = $('input[name="' + fullOtherName + '"]').val();
- var regEx = new RegExp("\\-","gi");
- other=other.replace(regEx,"/");
- var targetDate = new Date(other);
- value=value.replace(regEx,"/");
- var dt = new Date(value);
- return dt > targetDate;
- } catch (e) {
- return false;
- }
- }
- return true;
- });
- adapters.addSingleVal('notlessthan', 'otherproperty');
- $jQval.addMethod('rangeifidsin', function (value, element, params) {
- if (value) {
- try {
- var min = params.min,
- max = params.max,
- ids = params.ids,
- template = params.template,
- otherproperty = params.otherproperty;
- var otherVal = $('input[name="' + otherproperty + '"]').val();
- otherVal = template.replace('{0}', otherVal).toLowerCase();
- ids = ids.toLowerCase();
- value=parseInt(value);
- min=parseInt(min);
- max=parseInt(max);
- if (ids.indexOf(otherVal) >= 0) {
- return value >= min && value <= max;
- }
- } catch (e) {
- return false;
- }
- }
- return true;
- });
- adapters.RangeIfIdsIn = function (adapterName, otherproperty, ids, minimum, maximum, template) {
- return adapters.add(adapterName, [minimum, maximum, otherproperty, ids, template], function (options) {
- var min = options.params.minimum,
- max = options.params.maximum,
- ids = options.params.ids,
- template = options.params.template,
- otherproperty = options.params.otherproperty;
- var prefix = getModelPrefix(options.element.name);
- var fullOtherName = appendModelPrefix(otherproperty, prefix);
- if (min && max && ids && otherproperty) {
- setValidationValues(options
- , adapterName
- , {
- 'min': min, 'max': max, 'ids': ids, 'otherproperty': fullOtherName, 'template': template
- });
- }
- });
- };
- adapters.RangeIfIdsIn('rangeifidsin', 'otherproperty', 'ids', 'minimum', 'maximum', 'template');
- })();
- $(function () {
- if ($.fn.makeValidationEasyUI) {
- //$(".formitem").parents("form").makeValidationEasyUI();
- $("form").makeValidationEasyUI();
- }
- $('form .easyui-datebox[validtype="daterequired"],form .easyui-datetimebox[validtype="daterequired"]').each(function () {
- var message = $(this).attr("invalidmessage");
- $(this).validatebox("removeForCombobox");
- $(this).validatebox("reduceForCombobox");
- //$(this).focus(function () { $(this).validatebox({ required: false, validType: 'daterequired', invalidMessage: message }); });
- });
- $("form").bind("submit.form", function () {
- $(this).find('.easyui-datebox[validtype="daterequired"],.easyui-datetimebox[validtype="daterequired"]').each(function () {
- var message = $(this).attr("invalidmessage");
- $(this).validatebox({ required: false, validType: 'daterequired', invalidMessage: message });
- });
- });
- //$('form .easyui-datebox[validtype="daterequired"],form .easyui-datetimebox[validtype="daterequired"]').validatebox('disableValidation')
- //.focus(function () { $(this).validatebox('enableValidation'); })
- //.blur(function () { $(this).validatebox('validate') });
- });
- //----------------------------------------------
- // End MVC Validate Extend
- //-----------------------------------------------
|