Exemplo n.º 1
0
        protected override bool ConditionApplies(ModelInstance root)
        {
            // Get the allowed values
            var allowedValues = GetAllowedValues(root);

            // Always consider as valid when there are no allowed values
            if (allowedValues == null)
            {
                return(false);
            }

            // Value properties
            if (Property is ModelValueProperty)
            {
                // List
                if (Property.IsList)
                {
                    // Get the current property value
                    var values = root.GetValue((ModelValueProperty)Property) as IEnumerable;

                    // Determine whether the property value is in the list of allowed values
                    return(!(values == null || values.Cast <object>().All(value => allowedValues.Contains(value))));
                }

                // Value
                else
                {
                    // Get the current property value
                    var value = root.GetValue((ModelValueProperty)Property);

                    // Determine whether the property value is in the list of allowed values
                    return(!(value == null || allowedValues.Contains(value)));
                }
            }

            // Reference properties
            else
            {
                // List Property
                if (Property.IsList)
                {
                    // Get the current property value
                    ModelInstanceList values = root.GetList((ModelReferenceProperty)Property);

                    // Determine whether the property value is in the list of allowed values
                    return(!(values == null || values.All(value => allowedValues.Contains(value))));
                }

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

                    // Determine whether the property value is in the list of allowed values
                    return(!(value == null || allowedValues.Contains(value)));
                }
            }
        }
        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);
        }