Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Annotations for validation, at least one required field?

If I have a search object with a list of fields, can I, using the System.ComponentModel.DataAnnotations namespace, set it up to validate that at least one of the fields in the search is not null or empty? i.e All the fields are optional but at least one should always be entered.

like image 217
Boob Avatar asked Apr 26 '10 10:04

Boob


People also ask

How do you validate data annotations?

DataAnnotations namespace includes the following validator attributes: Range – Enables you to validate whether the value of a property falls between a specified range of values. RegularExpression – Enables you to validate whether the value of a property matches a specified regular expression pattern.

Which property of required data annotation is used to set the error message on validation?

ValidationAttribute, has an important property, ErrorMessage. This property get or set the custom validation message in case of error.

What is data annotation validator attributes in MVC?

Data annotation attributes are attached to the properties of the model class and enforce some validation criteria. They are capable of performing validation on the server side as well as on the client side. This article discusses the basics of using these attributes in an ASP.NET MVC application.


1 Answers

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)     {         PropertyInfo propertyInfo;         foreach (string propertyName in PropertyList)         {             propertyInfo = value.GetType().GetProperty(propertyName);              if (propertyInfo != null && propertyInfo.GetValue(value, null) != null)             {                 return true;             }         }          return false;     } } 

Usage:

[AtLeastOneProperty("StringProp", "Id", "BoolProp", ErrorMessage="You must supply at least one value")] public class SimpleTest {     public string StringProp { get; set; }     public int? Id { get; set; }     public bool? BoolProp { get; set; } } 

And if you want to have 2 groups (or more):

[AtLeastOneProperty("StringProp", "Id", ErrorMessage="You must supply at least one value")] [AtLeastOneProperty("BoolProp", "BoolPropNew", ErrorMessage="You must supply at least one value")] public class SimpleTest {     public string StringProp { get; set; }     public int? Id { get; set; }     public bool? BoolProp { get; set; }     public bool? BoolPropNew { get; set; } } 
like image 91
Valerio Gentile Avatar answered Sep 28 '22 13:09

Valerio Gentile