var EasyUIValidate = {
Events: {
ComboboxOnChange: function (newvalue, oldvalue) {
var $this = $(this);
var $form = $this.closest("form");
var $caller = $this.find(".combo-text");
if ($caller.hasClass("input-validation-error")) {
$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");
}
}
}
},
ComboGridXOnChange: function () {
var $this = $(this);
var $form = $this.closest("form");
var $caller = $this.find(".combo-text");
if ($caller.hasClass("input-validation-error")) {
var validator = $form.validate();
var vitem = $this.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");
}
}
}
}
}
};
(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 = $("
" + "" + "" + "" + "" + "
").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");
}
if ($caller.is(".combo-text") && !$caller.parent().hasClass("easyui-combogridX")) {
$caller.addClass("input-validation-error");
//$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
//-----------------------------------------------