//new method
        public IEnumerable <ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule();

            rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
            rule.ValidationParameters.Add("chars", _chars);
            rule.ValidationType = "customvalidationclass";
            yield return(rule);
        }
 public IEnumerable <System.Web.Mvc.ModelClientValidationRule> GetClientValidationRules(
     System.Web.Mvc.ModelMetadata metadata,
     System.Web.Mvc.ControllerContext context)
 {
     yield return(new System.Web.Mvc.ModelClientValidationRule
     {
         ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
         ValidationType = "booleanrequired"
     });
 }
        protected virtual void BindProperty(
            ControllerContext controllerContext,
            ModelBindingContext bindingContext,
            PropertyDescriptor propertyDescriptor
            )
        {
            // need to skip properties that aren't part of the request, else we might hit a StackOverflowException
            string fullPropertyKey = CreateSubPropertyName(
                bindingContext.ModelName,
                propertyDescriptor.Name
                );

            if (!bindingContext.ValueProvider.ContainsPrefix(fullPropertyKey))
            {
                return;
            }

            // call into the property's model binder
            IModelBinder  propertyBinder        = Binders.GetBinder(propertyDescriptor.PropertyType);
            object        originalPropertyValue = propertyDescriptor.GetValue(bindingContext.Model);
            ModelMetadata propertyMetadata      = bindingContext.PropertyMetadata[
                propertyDescriptor.Name
                                                  ];

            propertyMetadata.Model = originalPropertyValue;
            ModelBindingContext innerBindingContext = new ModelBindingContext()
            {
                ModelMetadata = propertyMetadata,
                ModelName     = fullPropertyKey,
                ModelState    = bindingContext.ModelState,
                ValueProvider = bindingContext.ValueProvider
            };
            object newPropertyValue = GetPropertyValue(
                controllerContext,
                innerBindingContext,
                propertyDescriptor,
                propertyBinder
                );

            propertyMetadata.Model = newPropertyValue;

            // validation
            ModelState modelState = bindingContext.ModelState[fullPropertyKey];

            if (modelState == null || modelState.Errors.Count == 0)
            {
                if (
                    OnPropertyValidating(
                        controllerContext,
                        bindingContext,
                        propertyDescriptor,
                        newPropertyValue
                        )
                    )
                {
                    SetProperty(
                        controllerContext,
                        bindingContext,
                        propertyDescriptor,
                        newPropertyValue
                        );
                    OnPropertyValidated(
                        controllerContext,
                        bindingContext,
                        propertyDescriptor,
                        newPropertyValue
                        );
                }
            }
            else
            {
                SetProperty(
                    controllerContext,
                    bindingContext,
                    propertyDescriptor,
                    newPropertyValue
                    );

                // Convert FormatExceptions (type conversion failures) into InvalidValue messages
                foreach (
                    ModelError error in modelState.Errors
                    .Where(
                        err => String.IsNullOrEmpty(err.ErrorMessage) && err.Exception != null
                        )
                    .ToList()
                    )
                {
                    for (
                        Exception exception = error.Exception;
                        exception != null;
                        exception = exception.InnerException
                        )
                    {
                        // We only consider "known" type of exception and do not make too aggressive changes here
                        if (exception is FormatException || exception is OverflowException)
                        {
                            string displayName          = propertyMetadata.GetDisplayName();
                            string errorMessageTemplate = GetValueInvalidResource(
                                controllerContext
                                );
                            string errorMessage = String.Format(
                                CultureInfo.CurrentCulture,
                                errorMessageTemplate,
                                modelState.Value.AttemptedValue,
                                displayName
                                );
                            modelState.Errors.Remove(error);
                            modelState.Errors.Add(errorMessage);
                            break;
                        }
                    }
                }
            }
        }
示例#4
0
 public IEnumerable <ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
 {
     yield return(new ModelClientValidationRemoteRule(FormatErrorMessage(metadata.GetDisplayName()), GetUrl(context), HttpMethod, FormatAdditionalFieldsForClientValidation(metadata.PropertyName)));
 }
示例#5
0
 public IEnumerable <ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
 {
     yield return(new ModelClientValidationEqualToRule(FormatErrorMessage(metadata.GetDisplayName()), FormatPropertyForClientValidation(OtherProperty)));
 }