public ModelValidatingEventArgs(ModelBindingExecutionContext modelBindingExecutionContext, ModelValidationNode parentNode) { if (modelBindingExecutionContext == null) { throw new ArgumentNullException("modelBindingExecutionContext"); } ModelBindingExecutionContext = modelBindingExecutionContext; ParentNode = parentNode; }
public ComplexModelResult(object model, ModelValidationNode validationNode) { if (validationNode == null) { throw new ArgumentNullException("validationNode"); } Model = model; ValidationNode = validationNode; }
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); } } }
public ModelValidatedEventArgs(ModelBindingExecutionContext modelBindingExecutionContext, ModelValidationNode parentNode) { if (modelBindingExecutionContext == null) { throw new ArgumentNullException("modelBindingExecutionContext"); } ModelBindingExecutionContext = modelBindingExecutionContext; ParentNode = parentNode; }
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; }
// 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); } } }); }
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); }
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); }
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); } } }