public GettextModelValidator(ModelMetadata metadata, ControllerContext controllerContext, ModelValidator implementation)
     : base(metadata, controllerContext)
 {
     if (implementation == null) throw new ArgumentNullException("implementation");
     this.implementation = implementation;
 }
 public ModelValidatorWrapper(ModelValidator modelValidator, ModelMetadata metadata, ControllerContext controllerContext)
     : base(metadata, controllerContext)
 {
     InnerValidator = modelValidator;
 }
 public LocalizableDataAnnotationsModelValidator(ModelValidator innerValidator, ModelMetadata metadata, ControllerContext controllerContext)
     : base(metadata, controllerContext)
 {
     _innerValidator = innerValidator;
     _metadata = metadata;
 }
            public WrapNumericModelValidator(ModelMetadata metadata, ControllerContext controllerContext, ModelValidator inner, string conditionProperty, bool validateIfNot)
                : base(metadata, controllerContext)
            {
                _inner = inner;
                _condtionProperty = conditionProperty;
                _validateIfNot = validateIfNot;

                BaseAttributeAdapter<RequiredAttribute>.ExtractMetadata(controllerContext, metadata, out _valueProvider, out _propertyName, out _containerName, out _modelState);
            }
        private static IEnumerable<object> GetPropertyValidators(ModelValidator validator)
        {
            if (validator == null)
                throw new ArgumentNullException("validator");

            var rules = new List<object>();

            foreach (var rule in validator.GetClientValidationRules())
            {
                rules.Add(GetClientValidationRule(rule));
                rules.AddRange(GetClientValidationParameters(rule));
            }

            return rules;
        }
 public DelegatingFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, IPropertyValidator validator)
     : base(metadata, controllerContext, validator)
 {
     innerValidator = ExtendedFluentValidationModelValidatorProvider.GetModelValidatorFromFullList(metadata, controllerContext, ((IDelegatingValidator)validator).InnerValidator);
     ShouldValidate = false;
 }
Esempio n. 7
0
        protected virtual void SetProperty(
            ControllerContext controllerContext,
            ModelBindingContext bindingContext,
            PropertyDescriptor propertyDescriptor,
            object value
            )
        {
            ModelMetadata propertyMetadata = bindingContext.PropertyMetadata[
                propertyDescriptor.Name
                                             ];

            propertyMetadata.Model = value;
            string modelStateKey = CreateSubPropertyName(
                bindingContext.ModelName,
                propertyMetadata.PropertyName
                );

            // If the value is null, and the validation system can find a Required validator for
            // us, we'd prefer to run it before we attempt to set the value; otherwise, property
            // setters which throw on null (f.e., Entity Framework properties which are backed by
            // non-nullable strings in the DB) will get their error message in ahead of us.
            //
            // We are effectively using the special validator -- Required -- as a helper to the
            // binding system, which is why this code is here instead of in the Validating/Validated
            // methods, which are really the old-school validation hooks.
            if (value == null && bindingContext.ModelState.IsValidField(modelStateKey))
            {
                ModelValidator requiredValidator = ModelValidatorProviders.Providers
                                                   .GetValidators(propertyMetadata, controllerContext)
                                                   .Where(v => v.IsRequired)
                                                   .FirstOrDefault();
                if (requiredValidator != null)
                {
                    foreach (
                        ModelValidationResult validationResult in requiredValidator.Validate(
                            bindingContext.Model
                            )
                        )
                    {
                        bindingContext.ModelState.AddModelError(
                            modelStateKey,
                            validationResult.Message
                            );
                    }
                }
            }

            bool isNullValueOnNonNullableType =
                value == null && !TypeHelpers.TypeAllowsNullValue(propertyDescriptor.PropertyType);

            // Try to set a value into the property unless we know it will fail (read-only
            // properties and null values with non-nullable types)
            if (!propertyDescriptor.IsReadOnly && !isNullValueOnNonNullableType)
            {
                try
                {
                    propertyDescriptor.SetValue(bindingContext.Model, value);
                }
                catch (Exception ex)
                {
                    // Only add if we're not already invalid
                    if (bindingContext.ModelState.IsValidField(modelStateKey))
                    {
                        bindingContext.ModelState.AddModelError(modelStateKey, ex);
                    }
                }
            }

            // Last chance for an error on null values with non-nullable types, we'll use
            // the default "A value is required." message.
            if (
                isNullValueOnNonNullableType &&
                bindingContext.ModelState.IsValidField(modelStateKey)
                )
            {
                bindingContext.ModelState.AddModelError(
                    modelStateKey,
                    GetValueRequiredResource(controllerContext)
                    );
            }
        }