public void CreateValidationMetadata(ValidationMetadataProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.Key.ContainerType != null && !context.Key.ContainerType.IsValueType) { var viewConfig = ServiceLocator.GetViewConfigure(context.Key.ContainerType); if (viewConfig != null && context.Key.Name.IsNotNullAndWhiteSpace()) { var descriptor = viewConfig.GetViewPortDescriptor(context.Key.Name); if (descriptor != null) { descriptor.Validator.Each(v => { v.DisplayName = descriptor.DisplayName; if (v is RangeValidator) { RangeValidator valid = (RangeValidator)v; RangeAttribute range = new RangeAttribute(valid.Min, valid.Max); range.ErrorMessage = valid.ErrorMessage; context.ValidationMetadata.ValidatorMetadata.Add(range); } else if (v is RegularValidator) { RegularValidator valid = (RegularValidator)v; RegularExpressionAttribute regular = new RegularExpressionAttribute(valid.Expression); regular.ErrorMessage = valid.ErrorMessage; context.ValidationMetadata.ValidatorMetadata.Add(regular); } else if (v is RemoteValidator) { RemoteValidator valid = (RemoteValidator)v; RemoteAttribute remote = new RemoteAttribute(valid.Action, valid.Controller, valid.Area); remote.ErrorMessage = valid.ErrorMessage; context.ValidationMetadata.ValidatorMetadata.Add(remote); } else if (v is RequiredValidator) { RequiredValidator valid = (RequiredValidator)v; RequiredAttribute required = new RequiredAttribute(); required.ErrorMessage = valid.ErrorMessage; context.ValidationMetadata.ValidatorMetadata.Add(required); } else if (v is StringLengthValidator) { StringLengthValidator valid = (StringLengthValidator)v; StringLengthAttribute stringLength = new StringLengthAttribute(valid.Max); stringLength.ErrorMessage = valid.ErrorMessage; context.ValidationMetadata.ValidatorMetadata.Add(stringLength); } }); } } } }
public TextBoxDescriptor Remote(RemoteValidator validator) { this.Validator.Add(validator); return(this); }
public RemoteModelValidator(EasyModelMetaData metadata, ControllerContext context, RemoteValidator remotevalidator) : base(metadata, context) { this.area = remotevalidator.Area; this.action = remotevalidator.Action; this.controller = remotevalidator.Controller; this.Attribute = new RemoteAttribute(action, controller); //this.Attribute.ErrorMessageResourceType = Metadata.ContainerType; //this.Attribute.ErrorMessageResourceName = Metadata.PropertyName; this.Attribute.ErrorMessage = remotevalidator.ErrorMessage; this.Attribute.AdditionalFields = remotevalidator.AdditionalFields; }
public new HtmlTagBase Remote(RemoteValidator validator) { return(base.Remote(validator) as TextBoxHtmlTag); }
/// <summary> /// 远程验证 /// </summary> /// <param name="validator"></param> /// <returns></returns> public HtmlTagBase Remote(RemoteValidator validator) { this.Validator.Add(validator); return(this); }
private string ToHtmlString(bool widthLabel) { if (string.IsNullOrEmpty(this.DisplayName)) { this.DisplayName = this.Name; } string val = this.Value == null ? "" : this.Value.ToString().HtmlEncode(); StringBuilder builder = new StringBuilder(); if (widthLabel && this.TagType != HTMLEnumerate.HTMLTagTypes.Hidden && !this.IsHidden && !this.IsIgnore) { builder.AppendFormat("<span class=\"input-group-addon {1}\">{0}</span>", this.DisplayName, this.IsRequired ? "required" : ""); } if (this.TagType == HTMLEnumerate.HTMLTagTypes.File) { builder.AppendFormat("<input type=\"hidden\" name=\"{0}{1}\" id=\"{0}{1}\" value=\"{2}\" />", this.NamePreFix, this.Name, val); } builder.Append(StartStr); switch (this.TagType) { case HTMLEnumerate.HTMLTagTypes.PassWord: case HTMLEnumerate.HTMLTagTypes.Input: case HTMLEnumerate.HTMLTagTypes.Hidden: builder.AppendFormat(" value=\"{0}\" ", val); break; case HTMLEnumerate.HTMLTagTypes.CheckBox: { bool check = false; if (val == "") { check = false; } else { check = Convert.ToBoolean(val); } builder.AppendFormat(" {0} ", check ? "checked=\"checked\"" : ""); builder.Append(" value=\"true\" "); break; } } builder.AppendFormat(" id=\"{1}{0}\" name=\"{1}{0}\"", this.Name, this.NamePreFix); if (Validator.Count > 0) { this.AddProperty("data-val", "true"); } foreach (ValidatorBase item in Validator) { item.DisplayName = this.DisplayName; if (item is RequiredValidator) { this.AddProperty("data-val-required", item.ErrorMessage); this.AddClass("required"); } else if (item is RegularValidator) { this.AddProperty("data-val-regex", item.ErrorMessage); this.AddProperty("data-val-regex-pattern", (item as RegularValidator).Expression); } else if (item is RemoteValidator) { RemoteValidator temp = item as RemoteValidator; this.AddProperty("data-val-remote", temp.ErrorMessage); this.AddProperty("data-val-remote-additionalfields", temp.AdditionalFields); this.AddProperty("data-val-remote-type", ""); this.AddProperty("data-val-remote-url", temp.Url); } else if (item is RangeValidator) { RangeValidator temp = item as RangeValidator; this.AddProperty("data-val-range", temp.ErrorMessage); this.AddProperty("data-val-range-max", temp.Max.ToString()); this.AddProperty("data-val-range-min", temp.Min.ToString()); } else if (item is StringLengthValidator) { StringLengthValidator temp = item as StringLengthValidator; this.AddProperty("data-val-length", temp.ErrorMessage); this.AddProperty("data-val-length-max", temp.Max.ToString()); } } foreach (var item in this.Properties) { builder.AppendFormat(" {0}=\"{1}\"", item.Key, item.Value); } if (Styles.Count > 0) { builder.Append(" style=\""); foreach (var item in Styles) { builder.AppendFormat("{0}:{1};", item.Key, item.Value); } builder.Append("\""); } if (Classes.Count > 0) { builder.Append(" class=\""); int i = 0; foreach (var item in Classes) { if (i == 0) { builder.AppendFormat("{0}", item); } else { builder.AppendFormat(" {0}", item); } i++; } builder.Append("\""); } builder.Append(EndStr); if (!this.IsHidden && this.TagType != HTMLEnumerate.HTMLTagTypes.Hidden) { builder.AppendFormat(errorMsgPlace, this.Name); } return(builder.ToString()); }