Upgrade from 4.1 to 5.0 generating attribute error for [IgnoreNulls]

Topics: Validation Application Block
Apr 8, 2011 at 12:26 AM

We've upgrade out .net project to .net 4/Entlib 5.0 from 3.5/4.1.  We've started to encounter a error:

 

The code in Question:
The logic being, if the Address isn't null then it should be less than 100.



  /// <summary>
        /// Gets or sets the Address2 property.
        /// </summary>
        [IgnoreNulls]
        [StringLengthValidator(1, 100, Tag = Address2Length)]
        [DataMember(IsRequired = true, Order = 3)]
        public string Address2 { get; set; }

generates the following type of error

System.NotSupportedException was unhandled by user code
  Message=A validation attribute of type IgnoreNullsAttribute cannot be used to validate values.
  Source=Microsoft.Practices.EnterpriseLibrary.Validation
  StackTrace:
       at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.BaseValidationAttribute.IsValid(Object value) in e:\Builds\EntLib\Latest\Source\Blocks\Validation\Src\Validation\Validators\BaseValidationAttribute.cs:line 184
       at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
       at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext)
       at System.Web.Mvc.DataAnnotationsModelValidator.<Validate>d__1.MoveNext()
       at System.Web.Mvc.ModelValidator.CompositeModelValidator.<Validate>d__5.MoveNext()
       at System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
       at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model)
       at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
       at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
       at System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
       at System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
       at System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext)
       at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model)
       at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
       at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
       at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
       at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
       at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  InnerException:

 

We've tried the following w/out success

 [ValidatorComposition(CompositionType.Or,
       MessageTemplate = "Street Address can be up to 100 characters.")]
        [NotNullValidator(Negated = true,MessageTemplate = "can be up to 100 characters.")]
        [StringLengthValidator(1,RangeBoundaryType.Ignore, 100, RangeBoundaryType.Inclusive, Tag = Address1Length)]
        [DataMember(IsRequired = true, Order = 2)]
        public string Address1 { get; set; }

The IgnoreNulls also fails when it's the sole dectorator.

 

Thanks for any guidance.

Apr 8, 2011 at 5:05 AM

after a little more sleuthing, I think this direction looks promising...  http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html

Apr 8, 2011 at 8:35 AM

Yes, that solution would work.  The reason behind the error was explained by Chris Tavares in this thread (with another suggested workaround).  In reality, the IgnoreNulls attribute maps to an OR composition of a negated not null attribute and the other specified validator. 

 

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

Apr 8, 2011 at 9:21 PM

I took the route of creating custom validators.  I hit one road block  -- ObjectValidator.  Can I make it so it ignores nulls?  if so, what method needs overriding?

Apr 11, 2011 at 8:00 AM

Hi,

That is the current behavior of the ObjectValidator. What is the exact issue you are encountering with ObjectValidator?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com