/// <summary> /// When implemented in a derived class, validates the object. /// </summary> /// <returns> /// A list of validation results. /// </returns> /// <param name="container">The container.</param> public override IEnumerable <ModelValidationResult> Validate(object container) { if (ShouldValidate) { ModelMetadata metadata = Metadata; bool isModelProperty = ModelMetadataHelper.IsModelProperty(metadata); ValidationResult result = ValidationRule.Validate(isModelProperty ? metadata.Model : container); ValidationErrorCollection errors = result.Errors; for (int i = 0; i < errors.Count; i++) { ValidationError error = errors[i]; yield return(new ModelValidationResult { Message = error.Message, MemberName = error.PropertyName }); } } }
/// <summary> /// Gets a list of validators. /// </summary> /// <returns> /// A list of validators. /// </returns> /// <param name="metadata">The metadata.</param><param name="context">The context.</param> public override IEnumerable <ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context) { if (metadata == null) { throw new ArgumentNullException("metadata"); } bool isModelProperty = ModelMetadataHelper.IsModelProperty(metadata); Type modelType = isModelProperty ? metadata.ContainerType : metadata.ModelType; IValidationTransformer validationTransformer = GetValidationTransformer(modelType); IEntityValidator validator = ValidatorFactory.GetValidatorForOptional(validationTransformer == null ? modelType : validationTransformer.ValidationModelType); if (isModelProperty) { return(GetValidatorsForProperty(metadata, context, validator, validationTransformer)); } return(GetValidatorsForModel(metadata, context, validator, validationTransformer)); }