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); } }); } } } }
// RegularController makes demands about its dependencies. public RegularController() { repository = new RegularRepository(); validator = new RegularValidator(); }
public RegularModelValidator(EasyModelMetaData metadata, ControllerContext context, RegularValidator regularvalidator) : base(metadata, context) { this.Attribute = new RegularExpressionAttribute(regularvalidator.Expression); //this.Attribute.ErrorMessageResourceType = Metadata.ContainerType; //this.Attribute.ErrorMessageResourceName = Metadata.PropertyName; this.Attribute.ErrorMessage = regularvalidator.ErrorMessage; }