DataAnnotations: Recursively validating an entire object graph

Here’s an alternative to the opt-in attribute approach. I believe this will traverse the object-graph properly and validate everything. public bool TryValidateObjectRecursive<T>(T obj, List<ValidationResult> results) { bool result = TryValidateObject(obj, results); var properties = obj.GetType().GetProperties().Where(prop => prop.CanRead && !prop.GetCustomAttributes(typeof(SkipRecursiveValidation), false).Any() && prop.GetIndexParameters().Length == 0).ToList(); foreach (var property in properties) { if (property.PropertyType == typeof(string) || … Read more

Unit Testing ASP.NET DataAnnotations validation

I posted this in my blog post: using System.ComponentModel.DataAnnotations; // model class public class Fiz { [Required] public string Name { get; set; } [Required] [RegularExpression(“.+@..+”)] public string Email { get; set; } } // in test class [TestMethod] public void EmailRequired() { var fiz = new Fiz { Name = “asdf”, Email = null … Read more

Conditionally required property using data annotations

RequiredIf validation attribute I’ve written a RequiredIfAttribute that requires a particular property value when a different property has a certain value (what you require) or when a different property has anything but a specific value. This is the code that may help: /// <summary> /// Provides conditional validation based on related property value. /// </summary> … Read more

MVC Model require true

I would create a validator for both Server AND Client side. Using MVC and unobtrusive form validation, this can be achieved simply by doing the following: Firstly, create a class in your project to perform the server side validation like so: public class EnforceTrueAttribute : ValidationAttribute, IClientValidatable { public override bool IsValid(object value) { if … Read more

ASP.NET MVC: Custom Validation by DataAnnotation

You could write a custom validation attribute: public class CombinedMinLengthAttribute: ValidationAttribute { public CombinedMinLengthAttribute(int minLength, params string[] propertyNames) { this.PropertyNames = propertyNames; this.MinLength = minLength; } public string[] PropertyNames { get; private set; } public int MinLength { get; private set; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var properties = this.PropertyNames.Select(validationContext.ObjectType.GetProperty); var … Read more

Why can’t I reference System.ComponentModel.DataAnnotations?

You have to reference the assembly in which this namespace is defined (it is not referenced by default in the visual studio templates). Open your reference manager and add a reference to the System.ComponentModel.DataAnnotations assembly (Solution explorer -> Add reference -> Select .Net tab -> select System.ComponentModel.DataAnnotations from the list)

Int or Number DataType for DataAnnotation validation attribute

For any number validation you have to use different different range validation as per your requirements : For Integer [Range(0, int.MaxValue, ErrorMessage = “Please enter valid integer Number”)] for float [Range(0, float.MaxValue, ErrorMessage = “Please enter valid float Number”)] for double [Range(0, double.MaxValue, ErrorMessage = “Please enter valid doubleNumber”)]