private string GenerateChildModels(Type type, KnockoutValidationOptions options, string namespaceName, bool genateChildModels) { var result = new StringBuilder(); if (genateChildModels) { foreach (var prop in type.GetProperties()) { if (IsCollection(prop.PropertyType)) { continue; } if (!IsPrimitiveType(prop.PropertyType)) { result.Append(new HtmlString(string.Format(ModelRootFormatString, prop.Name + "Base", BulidValidationBody(prop.PropertyType, prop.Name, options, true), namespaceName, SetUpDisplayPropertiesNames(prop.PropertyType), CreateValidatablePropertiesArray(prop.PropertyType), GenerateChildModels(prop.PropertyType, options, namespaceName, true)))); } } result = FormatString(result); return(result.ToString()); } return(string.Empty); }
private string BulidValidationBody(Type type, string idPrefixs, KnockoutValidationOptions options, bool generateChildModels) { var result = new StringBuilder(); result.Append(string.Format("ko.validation.init({{errorElementClass: 'error'," + " decorateElement: {3}," + " decorateInputElement: true," + " registerExtenders: {0}," + " insertMessages: {2} }});", options.RegisterExtenders.ToString().ToLower(), options.MessagesOnModified.ToString().ToLower(), options.InsertMessages.ToString().ToLower(), options.DecarateInput.ToString().ToLower())); result.Append("model.ValidatingChildren=[];"); result.Append("model.SetUpAllServerErrors = function(errors) {" + "for (var name in errors) {" + "if (name.indexOf('.') != -1) {continue;}" + "var value = errors[name];" + "model[name].SetUpServerError(errors[name], name);" + "model.Errors.showAllMessages();} "+ "var childProperties = splitPropertyAndChildModelsKeys(errors);if (childProperties.length >0) {" + "for (var i = 0; i < childProperties.length; i++) {" + "for (var name1 in childProperties[i]) {" + " if (name1 != undefined) {" + "model[childProperties[i][name1].Key].SetUpAllServerErrors(childProperties[i][name1].Data);" + "}" + "}}}};"); foreach (var prop in type.GetProperties()) { if (!generateChildModels && (!IsPrimitiveType(prop.PropertyType) || IsCollection(prop.PropertyType))) { continue; } if (generateChildModels && !IsPrimitiveType(prop.PropertyType) && !IsCollection(prop.PropertyType)) { result.AppendFormat("section.{1}.apply(model['{0}']);", prop.Name, prop.Name + "Base"); result.AppendFormat("model.ValidatingChildren.push(model['{0}']);", prop.Name); continue; } result.Append(BuildAttributeBasedValidation(prop.GetCustomAttributes(typeof(ValidationAttribute), true).Cast <ValidationAttribute>().ToList(), prop.Name)); result.Append(ExstendForServerValidation(prop.Name)); result.Append(SetUpDisplayPropertyName(prop, prop.Name)); result.Append(SetUpElementIdAndName(prop, prop.Name, idPrefixs)); } result.Append("model.GoToField = function (data) {" + "var elementId = '#' + data.Id;" + "var navbar = $('.navbar');var navbarheight = 10;" + "if (navbar.length > 0)navbarheight += navbar.height();" + "$('html, body').animate({scrollTop: $(elementId).offset().top - navbarheight}, 500);" + "setTimeout(function () { $('#' + data.Id).focus(); }, 500);};"); result.Append("model.IsValid = function () {var result = true;" + "for (var name in model) {" + "if (model[name]['__valid__'] != undefined) {" + "model[name].notifySubscribers('');" + "if (!model[name].isValid()) {" + "result = false;}" + "}" + "if (model.ValidatingChildren != undefined && model.ValidatingChildren.length > 0) {for (var i = 0; i < model.ValidatingChildren.length; i++) {" + " var child = model.ValidatingChildren[i];" + " if (!child.IsValid()) { result = false;}}}" + "}" + "return result;};"); BuildChildPropertiesValidationKeysSet(result); result = FormatString(result); return(result.ToString()); }
public string GenerateValidationModel(Type type, string modelName, string namespaceName, KnockoutValidationOptions options = null, bool genateChildModels = false, string idPrefixs = "") { return(string.Format(ModelRootFormatString, modelName, BulidValidationBody(type, idPrefixs, options, genateChildModels), namespaceName, SetUpDisplayPropertiesNames(type), CreateValidatablePropertiesArray(type), GenerateChildModels(type, options, namespaceName, genateChildModels))); }