public static void SetupMvcDefaults(bool setupValidation = true, bool setupMetadata = true) { var manager = LocalizationHelper.TextManager; if (setupValidation) { ModelBinders.Binders.DefaultBinder = new LocalizingDefaultModelBinder(manager); DataAnnotationsModelValidatorProvider.RegisterDefaultAdapterFactory((meta, ctx, attr) => new LocalizingDataAnnotationsModelValidator(meta, ctx, attr)); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(RangeAttribute), (meta, ctx, attr) => new LocalizingRangeAttributeAdapater(meta, ctx, attr)); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(RegularExpressionAttribute), (meta, ctx, attr) => new LocalizingRegularExpressionAttributeAdapater(meta, ctx, attr)); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(RequiredAttribute), (meta, ctx, attr) => new LocalizingRequiredAttributeAdapater(meta, ctx, attr)); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(StringLengthAttribute), (meta, ctx, attr) => new LocalizingStringLengthAttributeAdapater(meta, ctx, attr)); } if (setupMetadata) { ModelMetadataProviders.Current = new LocalizingModelMetadataProvider(); } }
static ChineseOnlyAttribute() { DataAnnotationsModelValidatorProvider.RegisterAdapterFactory( typeof(ChineseOnlyAttribute), (metadata, context, attribute) => new RegularExpressionAttributeAdapter(metadata, context, (RegularExpressionAttribute)attribute) ); }
protected void Application_Start() { //RouteTable.Routes.MapHubs(); AutoFacConfig.AutoFacConfigureContainer(); AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); //ModelBinders.Binders.DefaultBinder = new MyDefaultModelBinder(); ModelBinders.Binders.Add(typeof(DateTime?), new MyDateTimeModelBinder()); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(RoleRequiredAttribute), (metadata, controllerContext, attribute) => new RoleRequiredAttributeAdapter(metadata, controllerContext, (RoleRequiredAttribute)attribute)); // http://ben.onfabrik.com/posts/content-negotiation-in-aspnet-mvc ViewResultFormatters.Formatters.Add(new PartialViewResultFormatter()); AutoMapperConfiguration.Configure(); _logger.Trace("Application Starting Up - Application_Start()"); }
public void RegisterAdapterFactoryGuardClauses() { DataAnnotationsModelValidationFactory factory = (metadata, context, attribute) => null; // Attribute type cannot be null Assert.ThrowsArgumentNull( () => DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(null, factory), "attributeType" ); // Factory cannot be null Assert.ThrowsArgumentNull( () => DataAnnotationsModelValidatorProvider.RegisterAdapterFactory( typeof(MyValidationAttribute), null ), "factory" ); // Validation attribute must derive from ValidationAttribute Assert.Throws <ArgumentException>( () => DataAnnotationsModelValidatorProvider.RegisterAdapterFactory( typeof(object), factory ), "The type System.Object must derive from System.ComponentModel.DataAnnotations.ValidationAttribute\r\nParameter name: attributeType" ); }
public void RegisterAdapterFactory() { var oldFactories = DataAnnotationsModelValidatorProvider.AttributeFactories; try { // Arrange DataAnnotationsModelValidatorProvider.AttributeFactories = new Dictionary <Type, DataAnnotationsModelValidationFactory>(); DataAnnotationsModelValidationFactory factory = delegate { return(null); }; // Act DataAnnotationsModelValidatorProvider.RegisterAdapterFactory( typeof(MyValidationAttribute), factory ); // Assert var type = DataAnnotationsModelValidatorProvider.AttributeFactories.Keys.Single(); Assert.Equal(typeof(MyValidationAttribute), type); Assert.Same( factory, DataAnnotationsModelValidatorProvider.AttributeFactories.Values.Single() ); } finally { DataAnnotationsModelValidatorProvider.AttributeFactories = oldFactories; } }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory( typeof(RequiredAttribute), (metadata, controllerContext, attribute) => new FixedRequiredAttributeAdapter(metadata, controllerContext, (RequiredAttribute)attribute) ); //set the antiforgery claim to user id AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier; }
public static void RegisterLocalizationServices() { ModelMetadataProviders.Current = new LocalizedModelMetadataProvider(); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RangeAttribute), typeof(LocalizedRangeAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RegularExpressionAttribute), typeof(LocalizedRegularExpressionAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute), typeof(LocalizedRequiredAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(StringLengthAttribute), typeof(LocalizedStringLengthAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MaxLengthAttribute), typeof(LocalizedMaxLengthAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MinLengthAttribute), typeof(LocalizedMinLengthAttributeAdapter)); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(CreditCardAttribute), (metadata, context, attribute) => new LocalizedDataTypeAttributeAdapter(metadata, context, (DataTypeAttribute)attribute, "creditcard")); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(EmailAddressAttribute), (metadata, context, attribute) => new LocalizedDataTypeAttributeAdapter(metadata, context, (DataTypeAttribute)attribute, "email")); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(PhoneAttribute), (metadata, context, attribute) => new LocalizedDataTypeAttributeAdapter(metadata, context, (DataTypeAttribute)attribute, "phone")); DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(UrlAttribute), (metadata, context, attribute) => new LocalizedDataTypeAttributeAdapter(metadata, context, (DataTypeAttribute)attribute, "url")); }
public override void Boot(RouteCollection routes) { base.Boot(routes); //we requrie that a custom GlobalFilter is added so see if it is there: if (!GlobalFilters.Filters.ContainsFilter <ProxyableResultAttribute>()) { GlobalFilters.Filters.Add(new ProxyableResultAttribute()); } routes.RegisterArea(_installRegistration); //register all component areas foreach (var c in _componentAreas) { routes.RegisterArea(c); } //IMPORTANT: We need to register the Rebel area after the components because routes overlap. // For example, a surface controller might have a url of: // /Rebel/MyPackage/Surface/MySurface // and because the default action is 'Index' its not required to be there, however this same route // matches the default Rebel back office route of Rebel/{controller}/{action}/{id} // so we want to make sure that the plugin routes are matched first routes.RegisterArea(_areaRegistration); //ensure that the IAttributeTypeRegistry is set AttributeTypeRegistry.SetCurrent(_attributeTypeRegistry); //register validation extensions DataAnnotationsModelValidatorProviderExtensions.RegisterValidationExtensions(); LocalizationWebConfig.RegisterRoutes(routes, _settings.RebelPaths.LocalizationPath); //If this is outside of an ASP.Net application (i.e. Unit test) and RegisterVirtualPathProvider is called then an exception is thrown. if (HostingEnvironment.IsHosted) { HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedViewVirtualPathProvider()); HostingEnvironment.RegisterVirtualPathProvider(new CodeDelegateVirtualPathProvider()); } //register custom validation adapters DataAnnotationsModelValidatorProvider.RegisterAdapterFactory( typeof(HiveIdRequiredAttribute), (metadata, controllerContext, attribute) => new RequiredHiveIdAttributeAdapter(metadata, controllerContext, (HiveIdRequiredAttribute)attribute)); }
/// <summary> /// Register model validator providers. /// </summary> public void Register() { // Register custom adapter for the [Required] attribute to include handling of SelectList, MultiSelectList, IEnumerable<SelectListItem> and bool with [Required] DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(RequiredAttribute), (metadata, controllerContext, attribute) => new InfrastructureRequiredAttributeAdapter(metadata, controllerContext, (RequiredAttribute)attribute)); }