Utility class that supports accessing the value of a property along a static or instance source path.
Пример #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            ModelInstance instance = ModelInstance.GetModelInstance(validationContext.ObjectInstance);

            // Get the member name by looking up using the display name, since the member name is mysteriously null for MVC3 projects
            var propertyName = validationContext.MemberName ?? validationContext.ObjectType.GetProperties()
                .Where(p => p.GetCustomAttributes(false).OfType<DisplayAttribute>()
                    .Any(a => a.Name == validationContext.DisplayName)).Select(p => p.Name).FirstOrDefault();

            ModelProperty sourceProperty = instance.Type.Properties[propertyName];

            if (ComparisonPropertyName == null)
                return null;

            ModelSource comparePropPath = new ModelSource(instance.Type, ComparisonPropertyName);

            object compareValue = comparePropPath.GetValue(instance);

            int comparison = ((IComparable)compareValue).CompareTo(value);
            switch (Operator)
            {
                case CompareOperator.Equal: return comparison == 0 ? null : new ValidationResult("Invalid value", new string[] { propertyName });
                case CompareOperator.NotEqual: return comparison != 0 ? null : new ValidationResult("Invalid value", new string[] { propertyName });
                case CompareOperator.GreaterThan: return comparison < 0 ? null : new ValidationResult("Invalid value", new string[] { propertyName });
                case CompareOperator.GreaterThanEqual: return comparison <= 0 ? null : new ValidationResult("Invalid value", new string[] { propertyName });
                case CompareOperator.LessThan: return comparison > 0 ? null : new ValidationResult("Invalid value", new string[] { propertyName });
                case CompareOperator.LessThanEqual: return comparison >= 0 ? null : new ValidationResult("Invalid value", new string[] { propertyName });
            }

            return null;
        }
Пример #2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var instance = ModelInstance.GetModelInstance(validationContext.ObjectInstance);
            var source = new ModelSource(instance.Type, Source);

            // Get the member name by looking up using the display name, since the member name is mysteriously null for MVC3 projects
            var propertyName = validationContext.MemberName ?? validationContext.ObjectType.GetProperties()
            .Where(p => p.GetCustomAttributes(false).OfType<DisplayAttribute>()
                .Any(a => a.Name == validationContext.DisplayName)).Select(p => p.Name).FirstOrDefault();

            var property = instance.Type.Properties[propertyName];

            // Get the list of allowed values
            ModelInstanceList allowedValues = source.GetList(instance);
            if (allowedValues == null)
            return null;

            // List Property
            if (property.IsList)
            {
            // Get the current property value
            ModelInstanceList items = instance.GetList((ModelReferenceProperty)property);

            // Determine whether the property value is in the list of allowed values
            if (!(items == null || items.All(item => allowedValues.Contains(item))))
                return new ValidationResult("Invalid value", new string[] { propertyName });
            }

            // Reference Property
            else
            {
            // Get the current property value
            ModelInstance item = instance.GetReference((ModelReferenceProperty)property);

            // Determine whether the property value is in the list of allowed values
            if (!(item == null || allowedValues.Contains(item)))
                return new ValidationResult("Invalid value", new string[] { propertyName });
            }

            return null;
        }
Пример #3
0
 /// <summary>
 /// Attempts to create a new <see cref="ModelSource"/> for the specified root type and path.
 /// </summary>
 /// <param name="rootType">The root type name, which is required for instance paths</param>
 /// <param name="path">The source path, which is either an instance path or a static path</param>
 /// <returns>True if the source was created, otherwise false</returns>
 public static bool TryGetSource(ModelType rootType, string path, out ModelSource source, out ModelProperty sourceProperty)
 {
     source = new ModelSource();
     if (source.InitializeFromTypeAndPath(rootType, path, out sourceProperty))
         return true;
     else
     {
         source = null;
         return false;
     }
 }