Last active
December 14, 2015 08:09
-
-
Save RobinHerbots/5055542 to your computer and use it in GitHub Desktop.
MVC DataAnnotations - Validate submodel in model with separate result in ModelState
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MvcApplication : HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
ModelValidatorProviders.Providers.Add(new RecursiveModelValidatorProvider()); | |
.... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Model | |
{ | |
[Required] | |
public string Name { get; set; } | |
[ValidateObjectAttribute] // set market attribute | |
public AddressModel Address { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace ModelValidation | |
{ | |
public class RecursiveModelValidator : ModelValidator | |
{ | |
public RecursiveModelValidator(ModelMetadata metadata, ControllerContext context, ValidateObjectAttribute attribute) : base(metadata, context) { } | |
public override IEnumerable<ModelValidationResult> Validate(object container) | |
{ | |
if (Metadata.Model != null) | |
{ | |
foreach (ModelMetadata propertyMetadata in Metadata.Properties) | |
{ | |
foreach (ModelValidator propertyValidator in propertyMetadata.GetValidators(ControllerContext)) | |
{ | |
foreach (ModelValidationResult propertyResult in propertyValidator.Validate(Metadata.Model)) | |
{ | |
yield return new ModelValidationResult | |
{ | |
MemberName = propertyMetadata.PropertyName, | |
Message = propertyResult.Message | |
}; | |
} | |
} | |
} | |
} | |
else { | |
if (Metadata.IsRequired) | |
{ | |
yield return new ModelValidationResult | |
{ | |
MemberName = Metadata.ModelType.ToString(), | |
Message = "The model may not be null." | |
}; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace ModelValidation | |
{ | |
/// <summary> | |
/// ModelValidatorProvider to enable recursive validation of submodels in the current model | |
/// </summary> | |
public class RecursiveModelValidatorProvider : AssociatedValidatorProvider | |
{ | |
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes) | |
{ | |
foreach (Attribute attribute in attributes) | |
{ | |
if (attribute is ValidateObjectAttribute) | |
yield return new RecursiveModelValidator(metadata, context, (ValidateObjectAttribute)attribute); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using System.Web; | |
namespace ModelValidation | |
{ | |
public class ValidateObjectAttribute : Attribute | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment