How can I tell the Data Annotations validator to also validate complex child properties?

Issue – Model Binder Order This is, unfortunately, the standard behavior of Validator.TryValidateObject which does not recursively validate the property values of the object As pointed out in Jeff Handley’s article on Validating Object and Properties with the Validator, by default, the validator will validate in order: Property-Level Attributes Object-Level Attributes Model-Level implementation IValidatableObject The … Read more

Custom model validation of dependent properties using Data Annotations

MVC2 comes with a sample “PropertiesMustMatchAttribute” that shows how to get DataAnnotations to work for you and it should work in both .NET 3.5 and .NET 4.0. That sample code looks like this: [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class PropertiesMustMatchAttribute : ValidationAttribute { private const string _defaultErrorMessage = “‘{0}’ and ‘{1}’ … Read more

Entity Framework code first unique column

In Entity Framework 6.1+ you can use this attribute on your model: [Index(IsUnique=true)] You can find it in this namespace: using System.ComponentModel.DataAnnotations.Schema; If your model field is a string, make sure it is not set to nvarchar(MAX) in SQL Server or you will see this error with Entity Framework Code First: Column ‘x’ in table … Read more

How to retrieve Data Annotations from code? (programmatically)

Extension method: public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute { var attrType = typeof(T); var property = instance.GetType().GetProperty(propertyName); return (T)property .GetCustomAttributes(attrType, false).First(); } Code: var name = player.GetAttributeFrom<DisplayAttribute>(nameof(player.PlayerDescription)).Name; var maxLength = player.GetAttributeFrom<MaxLengthAttribute>(nameof(player.PlayerName)).Length;

The specified value does not conform to the required format yyyy-MM-dd

The specifications for the HTML5 date picker state that the date must be in the format yyyy-MM-dd (ISO format). This means that you DisplayFormatAttribute must be [DisplayFormat(DataFormatString = “{0:yyyy-MM-dd}”, ApplyFormatInEditMode = true)] public string MyDate { get; set; } Alternatively you can manually add the format using @Html.TextBoxFor(m => m.MyDate, “{0:yyyy-MM-dd}”, new { @type = … Read more

How to create Custom Data Annotation Validators

To create a custom data annotation validator follow these gudelines: Your class has to inherit from System.ComponentModel.DataAnnotations.ValidationAttribute class. Override bool IsValid(object value) method and implement validation logic inside it. That’s it. IMPORTANT Caution Sometimes developers check that value is not null/empty and return false. This is usually incorrect behaviour, because that’s on Required validator to … Read more

How to change default validation error message in ASP.NET MVC?

Apparently my question was already answered at How to replace the default ModelState error message in Asp.net MVC 2? . I’ll summarize it here: Create App_GlobalResources folder for your project (right click to project -> Add -> Add ASP.NET folder -> App_GlobalResources). Add a resx file in that folder. Say MyNewResource.resx. Add resource key PropertyValueInvalid … Read more