コード例 #1
0
        IEnumerable <object> GetAllowedValues(ModelInstance root)
        {
            // Value properties
            if (Property is ModelValueProperty)
            {
                // Get the list of allowed values
                IEnumerable instances;
                if (modelSource != null)
                {
                    instances = modelSource.GetValue(root) as IEnumerable;
                }
                else
                {
                    instances = SourceExpression.Invoke(root) as IEnumerable;
                }
                return(instances != null?instances.Cast <object>().ToArray() : null);
            }

            // Reference properties
            else
            {
                // Get the model type of the property the rule applies to
                ModelType propertyType = ((ModelReferenceProperty)Property).PropertyType;

                // Get the list of allowed values
                if (modelSource != null)
                {
                    // Model Source
                    var instances = modelSource.GetList(root);
                    if (instances != null)
                    {
                        return(instances.ToArray());
                    }
                }
                else
                {
                    // Model Expression
                    var instances = SourceExpression.Invoke(root) as IEnumerable;
                    if (instances != null)
                    {
                        return(instances.Cast <object>().Select(i => propertyType.GetModelInstance(i)).ToArray());
                    }
                }
            }

            return(null);
        }
コード例 #2
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);
        }
コード例 #3
0
        protected override bool ConditionApplies(ModelInstance root)
        {
            var value = root[Property];

            // Exit immediately if the target property has a value
            if (RequiredValue == null && (value != null &&
                                          (!(Property is ModelReferenceProperty) || !Property.IsList || root.GetList((ModelReferenceProperty)Property).Count > 0)))
            {
                return(false);
            }

            // If the required value is specified then the value must equal the required value
            var requiredValueCondition = RequiredValue == null || !RequiredValue.Equals(value);

            // Invoke the ModelExpression if it exists
            if (RequiredExpression != null)
            {
                try
                {
                    return((bool)RequiredExpression.Invoke(root) && requiredValueCondition);
                }
                catch
                {
                    return(false);
                }
            }
            // If the value to compare is null, then evaluate whether the compare source has a value
            else if (CompareValue == null)
            {
                return((CompareOperator == CompareOperator.Equal ? !compareSource.HasValue(root) : compareSource.HasValue(root)) && requiredValueCondition);
            }

            // Otherwise, perform a comparison of the compare source relative to the compare value
            bool?result = CompareRule.Compare(compareSource.GetValue(root), CompareOperator, CompareValue);

            return(result.HasValue && result.Value && requiredValueCondition);
        }
コード例 #4
0
        /// <summary>
        /// Determines whether the value of the property is valid relative to the value of a comparison property.
        /// </summary>
        /// <param name="root"></param>
        /// <returns></returns>
        protected override bool ConditionApplies(ModelInstance root)
        {
            bool?result = Compare(root[Property], CompareOperator, compareSource.GetValue(root));

            return(result.HasValue && !result.Value);
        }