コード例 #1
0
        public ModelValidatingEventArgs(ModelBindingExecutionContext modelBindingExecutionContext, ModelValidationNode parentNode) {
            if (modelBindingExecutionContext == null) {
                throw new ArgumentNullException("modelBindingExecutionContext");
            }

            ModelBindingExecutionContext = modelBindingExecutionContext;
            ParentNode = parentNode;
        }
コード例 #2
0
        public ComplexModelResult(object model, ModelValidationNode validationNode) {
            if (validationNode == null) {
                throw new ArgumentNullException("validationNode");
            }

            Model = model;
            ValidationNode = validationNode;
        }
コード例 #3
0
 public void CombineWith(ModelValidationNode otherNode) {
     if (otherNode != null && !otherNode.SuppressValidation) {
         Validated += otherNode.Validated;
         Validating += otherNode.Validating;
         foreach (ModelValidationNode childNode in otherNode.ChildNodes) {
             ChildNodes.Add(childNode);
         }
     }
 }
コード例 #4
0
        public ModelValidatedEventArgs(ModelBindingExecutionContext modelBindingExecutionContext, ModelValidationNode parentNode)
        {
            if (modelBindingExecutionContext == null)
            {
                throw new ArgumentNullException("modelBindingExecutionContext");
            }

            ModelBindingExecutionContext = modelBindingExecutionContext;
            ParentNode = parentNode;
        }
コード例 #5
0
        public ComplexModelResult(object model, ModelValidationNode validationNode)
        {
            if (validationNode == null)
            {
                throw new ArgumentNullException("validationNode");
            }

            Model          = model;
            ValidationNode = validationNode;
        }
コード例 #6
0
 public void CombineWith(ModelValidationNode otherNode)
 {
     if (otherNode != null && !otherNode.SuppressValidation)
     {
         Validated  += otherNode.Validated;
         Validating += otherNode.Validating;
         foreach (ModelValidationNode childNode in otherNode.ChildNodes)
         {
             ChildNodes.Add(childNode);
         }
     }
 }
コード例 #7
0
        private object TryConvertContainerToMetadataType(ModelValidationNode parentNode) {
            if (parentNode != null) {
                object containerInstance = parentNode.ModelMetadata.Model;
                if (containerInstance != null) {
                    Type expectedContainerType = ModelMetadata.ContainerType;
                    if (expectedContainerType != null) {
                        if (expectedContainerType.IsInstanceOfType(containerInstance)) {
                            return containerInstance;
                        }
                    }
                }
            }

            return null;
        }
コード例 #8
0
        // Called when the property setter null check failed, allows us to add our own error message to ModelState.
        internal static EventHandler <ModelValidatedEventArgs> CreateNullCheckFailedHandler(ModelBindingExecutionContext modelBindingExecutionContext, ModelMetadata modelMetadata, object incomingValue)
        {
            return((sender, e) =>
            {
                ModelValidationNode validationNode = (ModelValidationNode)sender;
                ModelStateDictionary modelState = e.ModelBindingExecutionContext.ModelState;

                if (modelState.IsValidField(validationNode.ModelStateKey))
                {
                    string errorMessage = ModelBinderErrorMessageProviders.ValueRequiredErrorMessageProvider(modelBindingExecutionContext, modelMetadata, incomingValue);
                    if (errorMessage != null)
                    {
                        modelState.AddModelError(validationNode.ModelStateKey, errorMessage);
                    }
                }
            });
        }
コード例 #9
0
        private object TryConvertContainerToMetadataType(ModelValidationNode parentNode)
        {
            if (parentNode != null)
            {
                object containerInstance = parentNode.ModelMetadata.Model;
                if (containerInstance != null)
                {
                    Type expectedContainerType = ModelMetadata.ContainerType;
                    if (expectedContainerType != null)
                    {
                        if (expectedContainerType.IsInstanceOfType(containerInstance))
                        {
                            return(containerInstance);
                        }
                    }
                }
            }

            return(null);
        }
コード例 #10
0
        public void Validate(ModelBindingExecutionContext modelBindingExecutionContext, ModelValidationNode parentNode) {
            if (modelBindingExecutionContext == null) {
                throw new ArgumentNullException("modelBindingExecutionContext");
            }

            if (SuppressValidation) {
                // no-op
                return;
            }

            // pre-validation steps
            ModelValidatingEventArgs eValidating = new ModelValidatingEventArgs(modelBindingExecutionContext, parentNode);
            OnValidating(eValidating);
            if (eValidating.Cancel) {
                return;
            }

            ValidateChildren(modelBindingExecutionContext);
            ValidateThis(modelBindingExecutionContext, parentNode);

            // post-validation steps
            ModelValidatedEventArgs eValidated = new ModelValidatedEventArgs(modelBindingExecutionContext, parentNode);
            OnValidated(eValidated);
        }
コード例 #11
0
        private void ValidateThis(ModelBindingExecutionContext modelBindingExecutionContext, ModelValidationNode parentNode) {
            ModelStateDictionary modelState = modelBindingExecutionContext.ModelState;
            if (!modelState.IsValidField(ModelStateKey)) {
                return; // short-circuit
            }

            object container = TryConvertContainerToMetadataType(parentNode);
            foreach (ModelValidator validator in ModelMetadata.GetValidators(modelBindingExecutionContext)) {
                foreach (ModelValidationResult validationResult in validator.Validate(container)) {
                    string trueModelStateKey = ModelBinderUtil.CreatePropertyModelName(ModelStateKey, validationResult.MemberName);
                    modelState.AddModelError(trueModelStateKey, validationResult.Message);
                }
            }
        }
コード例 #12
0
        private void ValidateThis(ModelBindingExecutionContext modelBindingExecutionContext, ModelValidationNode parentNode)
        {
            ModelStateDictionary modelState = modelBindingExecutionContext.ModelState;

            if (!modelState.IsValidField(ModelStateKey))
            {
                return; // short-circuit
            }

            object container = TryConvertContainerToMetadataType(parentNode);

            foreach (ModelValidator validator in ModelMetadata.GetValidators(modelBindingExecutionContext))
            {
                foreach (ModelValidationResult validationResult in validator.Validate(container))
                {
                    string trueModelStateKey = ModelBinderUtil.CreatePropertyModelName(ModelStateKey, validationResult.MemberName);
                    modelState.AddModelError(trueModelStateKey, validationResult.Message);
                }
            }
        }
コード例 #13
0
        public void Validate(ModelBindingExecutionContext modelBindingExecutionContext, ModelValidationNode parentNode)
        {
            if (modelBindingExecutionContext == null)
            {
                throw new ArgumentNullException("modelBindingExecutionContext");
            }

            if (SuppressValidation)
            {
                // no-op
                return;
            }

            // pre-validation steps
            ModelValidatingEventArgs eValidating = new ModelValidatingEventArgs(modelBindingExecutionContext, parentNode);

            OnValidating(eValidating);
            if (eValidating.Cancel)
            {
                return;
            }

            ValidateChildren(modelBindingExecutionContext);
            ValidateThis(modelBindingExecutionContext, parentNode);

            // post-validation steps
            ModelValidatedEventArgs eValidated = new ModelValidatedEventArgs(modelBindingExecutionContext, parentNode);

            OnValidated(eValidated);
        }