Validate data using DataAnnotations with WPF & Entity Framework?

You can use the DataAnnotations.Validator class, as described here: http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx But if you’re using a “buddy” class for the metadata, you need to register that fact before you validate, as described here: http://forums.silverlight.net/forums/p/149264/377212.aspx TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List<ValidationResult> results = new List<ValidationResult>(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, … Read more

What does it mean for a property to be [Required] and nullable?

The reason for making a property nullable and marked with the [Required] attribute is to protect against under-posting attacks. It also allows you to display an initial empty value in the view rather than the default value for the property. This is typically done with value type properties in view models. An under-posting attack is … Read more

DataAnnotations StringLength Attribute MVC – without max value

Is there a way to only specify only a minimum length? Perhaps another attribute i can use? Using the standard data annotation, No. You must specify the MaximumLength. Only the other parameters are optional. In such a case, I’d recommend something like this: [StringLength(int.MaxValue, MinimumLength = 7)] You can also use a Regex (regular expression) … Read more

Entity Framework 4.1 InverseProperty Attribute

I add an example for the InversePropertyAttribute. It cannot only be used for relationships in self referencing entities (as in the example linked in Ladislav’s answer) but also in the “normal” case of relationships between different entities: public class Book { public int ID {get; set;} public string Title {get; set;} [InverseProperty(“Books”)] public Author Author … Read more

Validating DataAnnotations with Validator class

I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx MVC recognizes the MetaDataType attribute, but other projects do not. Before validating, you need to manually register the metadata class: TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Persona), typeof(Persona_Validation)), typeof(Persona)); ValidationContext context = new ValidationContext(p, null, null); List<ValidationResult> results = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(p, context, results, true);

Data Annotations for validation, at least one required field?

I have extended Zhaph answer to support grouping of properties. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class AtLeastOnePropertyAttribute : ValidationAttribute { private string[] PropertyList { get; set; } public AtLeastOnePropertyAttribute(params string[] propertyList) { this.PropertyList = propertyList; } //See http://stackoverflow.com/a/1365669 public override object TypeId { get { return this; } } public override bool IsValid(object value) { … Read more

The field must be a number. How to change this message to another language?

If you happen to be using ASP.NET MVC 4 onwards, check this post: Localizing Default Error Messages in ASP.NET MVC and WebForms Basically you have to add the following piece of code in your Application_Start() method in Global.asax: ClientDataTypeModelValidatorProvider.ResourceClassKey = “Messages”; DefaultModelBinder.ResourceClassKey = “Messages”; Right click your ASP.NET MVC project in Solution Explorer inside Visual … Read more