Validation for object types

Jan 19, 2012 at 8:16 PM
Edited Jan 19, 2012 at 8:18 PM

Currently, we have the idea that you put an attribute on a property:

[RequiredIf("PropertyName", true)]

Wondering if, using reflection, we can say something like:

[RequiredIf("PropertyName", true, ObjectProperty="IsActive")]

or similar to be able to represent object validation...

Or

[RequiredIf("PropertyName.IsActive", true)]

and then parse the "PropertyName.IsActive" to get the parts we need to reflect against the sub objects to get to the property we really want to validate... this won't affect the client-side, since most of the time, what we are doing there is binding the Values properties of Lists to the Ids of the lookups.

Just a thought...

May 10, 2012 at 4:59 PM
Edited May 10, 2012 at 5:28 PM

Yes, I would like this too... in fact, it looks like if you simply changed the GetDependentPropertyValue method in the CotingentValidationAttribute base class to this it would probably work:

        private object GetDependentPropertyValue(object container)
        {
            Type currentType = container.GetType();

            object value = container;
            foreach (string propertyName in DependentProperty.Split('.'))
            {
                System.Reflection.PropertyInfo property = currentType.GetProperty(propertyName);
                value = property.GetValue(value, null);
                currentType = property.PropertyType;
            }

            return value;
        }

May 10, 2012 at 7:40 PM

I like it. I'll work on implementing it, unit testing it, and getting it to work with client side validation. 

May 10, 2012 at 8:09 PM

Looking at your client side validation, it actually looked like this MIGHT work right out of the box... but again, I didn't actually check :-)

Also, that could use a null value check in there somewhere... as it stands, if one of the dependent objects in the container happens to be null, this would probably puke out.

May 10, 2012 at 8:10 PM
It didn't work right out of the box, but it was easy to get working. I'm about to check in the changes in a few minutes.
May 10, 2012 at 8:18 PM

Okay check out Change Set 90297

 

May 10, 2012 at 8:41 PM
Edited May 10, 2012 at 9:01 PM

I'd still recommend a null check, but glad to see the turn around! Something like this maybe? 

        private object GetDependentPropertyValue(object container)
        {
            var currentType = container.GetType();
            var value = container;

            foreach (string propertyName in DependentProperty.Split('.'))
            {
                if (value == null)
                {
                    break;
                }
                var property = currentType.GetProperty(propertyName);
                value = property.GetValue(value, null);
                currentType = property.PropertyType;
            }

            return value;
        }

Any chance of us getting an updated NuGet package anytime soon with this? Please?