A validation attribute of type ValidatorCompositionAttribute cannot be used to validate values.

Topics: Validation Application Block
Apr 29, 2010 at 7:03 PM
Edited Apr 29, 2010 at 7:26 PM

I get the error below if I try to use the attributes below. This is an ASP.NET MVC 2 web application. Also, does the VAB add validation errors to the model state automatically? This seems to be the case. If so, can I disable it so I can use a custom handler to display?

[ValidatorComposition(CompositionType.Or)]
[NotNullValidator(Negated = true)]
[StringLengthValidator(50, MessageTemplate = "Address must be less than 50 characters.")]

-------------------------------

Server Error in '/' Application. A validation attribute of type ValidatorCompositionAttribute cannot be used to validate values. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NotSupportedException: A validation attribute of type ValidatorCompositionAttribute cannot be used to validate values. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [NotSupportedException: A validation attribute of type ValidatorCompositionAttribute cannot be used to validate values.] Microsoft.Practices.EnterpriseLibrary.Validation.Validators.BaseValidationAttribute.IsValid(Object value) +155 System.Web.Mvc.<Validate>d__1.MoveNext() +56 System.Web.Mvc.<Validate>d__5.MoveNext() +315 System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) +656 System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +60 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1048 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +280 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +257 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +109 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314 System.Web.Mvc.Controller.ExecuteCore() +105 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679150 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Apr 30, 2010 at 3:56 AM
Edited Apr 30, 2010 at 4:30 AM

What version of entlib are you using? I'll see if I can repro it.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 30, 2010 at 5:05 AM

Ok, I assume you're using the 5.0 version.  In 4.1, it doesn't automatically add  the validation errors in the modelstate and the error regarding the validatorcomposition doesn't occur.  I'll see if I can find any workaround for these.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 30, 2010 at 5:55 AM

I assume you're using the default MVC modelbinder? In Entlib 5 we integrated VAB with System.ComponentModel.DataAnnotations. This means that the default model binder picks up our stuff automatically as just another dataannotation, and it feeds the validation errors into the model state. However, the down side is that DataAnnotations doesn't support OR composition, only AND.

If you want to support using OR, you'll need to use a custom model binder that kicks off VAB explicitly rather than going through the DataAnnotations API.

 

Apr 30, 2010 at 2:16 PM
ctavares wrote:

I assume you're using the default MVC modelbinder? In Entlib 5 we integrated VAB with System.ComponentModel.DataAnnotations. This means that the default model binder picks up our stuff automatically as just another dataannotation, and it feeds the validation errors into the model state. However, the down side is that DataAnnotations doesn't support OR composition, only AND.

If you want to support using OR, you'll need to use a custom model binder that kicks off VAB explicitly rather than going through the DataAnnotations API.

 

Yes, I am using Entlib 5.  This seems like a poor choice to me, maybe I am wrong, but it totally breaks using this with MVC out of the box.  Why would I use VAB when I can just use DataAnnotations?  I guess I will switch and write custom attributes if I need functionality similar to what I got in VAB.  This really sucks.  It also explains why I was getting double error messages.  Of course, if I use self validation, it wouldn't give double error messages because it doesn't seem to add itself to the modelbinder directly.

 

Apr 30, 2010 at 2:17 PM

Along, this same note, if you try to use the "Property Compare" validator with MVC, it throws an error also.  I guess this is because DataAnnotations doesn't support that validator either.