コード例 #1
0
 public ConditionalModelClientValidationRule(string errorMessage, ICValidation validation, ICValidationInternal internalValidation)
 {
     ErrorMessage = errorMessage;
     ValidationType = Setup(OriginalValidationType, ValidationParameters, internalValidation.ConditionProperty, validation.ValidateIfNot);
 }
コード例 #2
0
ファイル: Helpers.cs プロジェクト: sperling/MVC-CVal
        /// <summary>
        /// Main validation method.
        /// </summary>
        /// <param name="validation"></param>
        /// <param name="value"></param>
        /// <param name="validationContext"></param>
        /// <param name="baseFunction"></param>
        /// <param name="validationInternal"></param>
        /// <returns></returns>
        internal static ValidationResult CValidate(this ICValidation validation, object value, ValidationContext validationContext, Func<object, ValidationContext, ValidationResult> baseFunction, ICValidationInternal validationInternal)
        {
            // default to regular behavior if we don't have model.
            if (validationContext.ObjectInstance == null)
            {
                return baseFunction(value, validationContext);
            }

            var property = validationContext.ObjectInstance.GetType().GetProperty(validationInternal.PropertyName);
            var shouldValidate = ShouldValidate(validationInternal.ConditionProperty, validation.ValidateIfNot, validationInternal.PropertyName, validationInternal.ContainerName, validationContext.ObjectInstance, property, validationInternal.ValueProvider, validationInternal.ModelState);

            if (shouldValidate == null)
            {
                // value for condition property is not supplied.
                return new ValidationResult(PropertyNotFound(validationInternal.ConditionProperty));
            }
            else if (shouldValidate.Value)
            {
                return baseFunction(value, validationContext);
            }
            else
            {
                return null;
            }

            /*var valueProviderResult = validationInternal.ValueProvider.GetValue(validationInternal.ConditionProperty);

            if (valueProviderResult == null)
            {
                // value for condition property is not supplied.
                return new ValidationResult(String.Format(CultureInfo.CurrentCulture, "Could not find a property named {0}.", validationInternal.ConditionProperty));
            }

            bool shouldValidate = (bool)valueProviderResult.ConvertTo(_boolType);

            // invert ValidateIfNot will give us correct right condition for test.
            bool condition = !validation.ValidateIfNot;

            if (shouldValidate == condition)
            {
                // let the normal validation handle it.
                return baseFunction(value, validationContext);
            }
            else
            {
                // TODO:    what if ObjectInstance is null here?
                // set property on model equalt to default(T) where T is property type.
                var property = validationContext.ObjectInstance.GetType().GetProperty(validationInternal.PropertyName);
                var v = property.PropertyType.IsValueType ? Activator.CreateInstance(property.PropertyType) : null;
                property.SetValue(validationContext.ObjectInstance, v, null);

                // also set ViewData.ModelState entry to default(T) where T is property type.
                // or else Html.TextBoxFor() etc. will pick up value from request and render it.
                ModelState modelState;
                string key = validationInternal.ContainerName != "" ? validationInternal.ContainerName + "." + validationInternal.PropertyName : validationInternal.PropertyName;

                if (validationInternal.ModelState.TryGetValue(key, out modelState))
                {
                    if (modelState.Value != null)
                    {
                        // these properties has proteced setters, so need a bit of reflection to
                        // clear out.
                        var propertyDesc = modelState.Value.GetType().GetProperty("RawValue");
                        propertyDesc.SetValue(modelState.Value, v, null);

                        propertyDesc = modelState.Value.GetType().GetProperty("AttemptedValue");
                        propertyDesc.SetValue(modelState.Value, null, null);
                    }
                }

                return null;
            }*/
        }