Non reference type error being thrown when before it didnt

Topics: Validation Application Block
Feb 11, 2015 at 9:41 AM
Edited Feb 12, 2015 at 1:43 PM
I am using Enterprise Library 5.0 and i having trouble with one of the validators.

Here is a property that i have in the following class which represents my Business Entity:
  [HasSelfValidation]
  public class Customer 
  {
    [RequiredValidator]
    [IsPositiveIntValidator(MessageTemplate = "Invalid OwnerId.")]
    public int? OwnerId {get; set; }

  }
The error i am now getting is the following which is different from the one before:
  Value to validate is null but expected an instance of the non-reference type System.Int32. - Customer.OwnerId
I have a custom validator implementation and usually it calls the DefaultMessageTemplate code:
   /// <summary>
    /// Required validator to ensure value has been specified.
    /// </summary>
    [ConfigurationElementType(typeof(CustomValidatorData))]
    public class RequiredValidator : ValueValidator
    {

        public override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            var valid = true;

            if (objectToValidate == null)
            {
                valid = false;
            }
            else
            {
                var str = objectToValidate as string;
                if (str != null)
                {
                    valid = str.Trim().Length != 0;
                }
                var dt = objectToValidate as DateTime?;
                if (dt != null)
                {
                    valid = dt != DateTime.MinValue;
                }

            }

            if (valid == Negated)
            {
                LogValidationResult(validationResults, GetMessage(objectToValidate, key), currentTarget, key);
            }
        }


        protected override string DefaultMessageTemplate
        {
            get
            {
                return "The {1} must be specified";
            }
        }
    }
Can someone tell me why suddenly i am getting completely different error?
Feb 12, 2015 at 1:30 PM
Edited Feb 12, 2015 at 1:43 PM
I found out if i swap the attributes (see below) then my DefaultMessageTemplate error message is used which i want but i shouldn't have to swap the attributes as it was working before.
 [HasSelfValidation]
  public class Customer 
  {
    [IsPositiveIntValidator(MessageTemplate = "Invalid OwnerId.")]
    [RequiredValidator]
    public int? OwnerId {get; set; }

  }
Can anyone provide any thoughts on the above in terms why it is suddenly working?