Validation Block & MVC 2 ModelValidator Integration

Topics: Validation Application Block
May 5, 2010 at 8:58 PM
Edited May 5, 2010 at 8:59 PM

In a discussion thread over in an MVC blog site of Brad Wilson, MS ASP.NET developer here: http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html#comment-6a00e54fbd8c4988340133ed46daea970b is information about integrating the MVC 2 model Validation with the Enterprise Library Validation block.  I've ask the question if there's an issue in a constructor of a inheritable base object there (See snippet below).  I'd also ask the same here but differently as it relates to the Validator.Validate method.  Can it infer the base type in the case of a nullable object and validate appropriately?

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

Here's a head scratcher: When I have nullable data types (in my case date? or Boolean?), I get the following error: "
Value to validate is null but expected an instance of the non-reference type System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]. " I can't seem to track down if that's a configuration issue in the Enterprise Lib Validation (5.0) or the integration w/ the MVC validator providor model. Any insight?

Brad Wilson said in reply to Shane...

Nope. You may want to ask on the EntLib forums on CodePlex.

Good luck!

Shane said in reply to Brad Wilson...

Ok, I slightly mis-spoke when I said I recieved that 'error'. That was the message property in the validation result object in the 'ConvertResults' method you provided found in:

yield return new ModelValidationResult
{
Message = validationResult.Message,
MemberName = validationResult.Key
};
}
I think it's a strong indicator of an error, but one that's internally thrown.
I'm also thinking that when the : base class 'ModelValidator' constructor is involked,

public EntLibValidatorWrapper(ModelMetadata metadata, ControllerContext context, Validator validator)
: base(metadata, context)

setting the metadata (and when I examine the metadata.ModelType property, that evaluates to "{Name = "Nullable`1" FullName = "System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}", that the ModelValidator base object cannot properly evaluate the nullable data type. Given that nullable types need to have their type evaluated using something like "Nullable.GetUnderlyingType()", the ModelType isn't quite correct, it fails validation (internally thrown error?). That brings me back to the ModelValidator base class's constructor that recieves the metadata object and then involkes 'return ConvertResults(_validator.Validate(Metadata.Model));' in the override 'Validate' method. Since the ModelType is not, I'll use the word 'correct', the MVC ModelValidator's MetaData.Model object cannot be validated by EntLib.validate since it wasn't handed the correct data type (in this case, MetaData.ModelType) information as determined by the ModelValidator base class constructor. Any chance that this theory can be validated in the \System.Web.Mvc.dll code? Any suggestions to overcome this problem if that is correct?

May 6, 2010 at 7:56 AM

The error message can be reproduce if you try this:

int? num = 1;
Validator validator = ValidationFactory.CreateValidator(num.GetType());
num = null;
ValidationResults results = validator.Validate(num);

 However, if you do this on a custom type, validation will succeed.  I wouldn't know if this is a bug or not, better ask Microsoft folks.

May 10, 2010 at 4:53 PM

Ent Lib Team (validation block grp),

Can you confirm this is a 'bug' with the validation application block?  Do you have any suggestions for a work around 'other' than don't use nullable types?!

 

thank you,

Shane

May 11, 2010 at 3:26 AM

This doesn't happen in the 4.1 version.  We'll confirm this with the entlib team.

 

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

May 24, 2010 at 5:20 PM

I'm still looking to see if there's a solution/bug.  Any word from the entlib team?

May 25, 2010 at 12:38 AM

None yet Shane, they usually reply directly to the thread.

 

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

Jul 22, 2010 at 8:18 PM

Hello, EntLib Team?

Could you please address this question/issue?

Thanks

Nov 16, 2010 at 7:42 AM
Edited Nov 16, 2010 at 7:47 AM

Bump.

I stumbled upon this problem in a different scenario, but the underlying problem seems to be the same. After taking a quick look at the source source code I found that EntLib v5 added an extra check compared to v4 in Validator.Generic.cs:

       /// <summary>
        /// Implements the validation logic for the receiver.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="currentTarget">The object on the behalf of which the validation is performed.</param>
        /// <param name="key">The key that identifies the source of <paramref name="objectToValidate"/>.</param>
        /// <param name="validationResults">The validation results to which the outcome of the validation should be stored.</param>
        /// <remarks>
        /// The implementation for this method will perform type checking and converstion before forwarding the 
        /// validation request to method <see cref="Validator{T}.DoValidate(T, object, string, ValidationResults)"/>.
        /// </remarks>
        /// <see cref="Validator.DoValidate"/>
        public override void DoValidate(object objectToValidate,
            object currentTarget,
            string key,
            ValidationResults validationResults)
        {
            // null values need to be avoided when checking for type compliance for value types
            if (objectToValidate == null)
            {
                if (typeof(T).IsValueType)
                {
                    string message
                        = string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.ExceptionValidatingNullOnValueType,
                            typeof(T).FullName);
                    LogValidationResult(validationResults, message, currentTarget, key);
                    return;
                }
            }
            ... the rest as in v4 ...

In the example by entlibrocks above int? is a value type that can be assigned the value null so this check kicks in and validation fails. Any comments from EntLib team?

Nov 18, 2010 at 11:47 AM

Is this issue being looked at?

Nov 18, 2010 at 8:34 PM

Yes, it is a bug and will be addressed in the next version. There's no plans at this time to do a patch release.

 

Apr 6, 2012 at 7:39 PM

Which version(s) does this apply to, and which version will it be addressed?  It's been 6 months since last posting here.

I started using Validation Block 5.0 yesterday, and this is the 2nd buggy behavior I've encountered so far.  Not instilling me w/ confidence people.  Not what I would call "predictable results" either.

Apr 8, 2012 at 9:27 AM

This applies to Enterprise Library 5 (releases 5.0.414.0 & 5.0.505.0).  The next release of Enterprise Library looks like Version 6 which is scheduled for this year (according to this roadmap).

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 11, 2012 at 8:12 PM

Has anyone identifies a work-around for this issue?  I've tried the Not-Null validator, with and without an OrComposite validator, but I still get this error when attempting to validate a nullable type (in my case, long?).

 

Thanks,

Phil

May 16, 2012 at 6:15 AM

I believe EL Validation Application Block is getting Outdated... Please correct me if I misunderstood.

 

Many Thanks,

Suraj

Jul 17, 2014 at 9:31 AM
This is still happening in EntLib Validation block 6.0 with WCF integration.