Data Annotation Attributes
- 6 minutes to read
DevExpress ASP.NET MVC components support data annotation attributes. You can apply these attributes to classes to specify validation rules and data display mode, set relationships between classes, etc.
Declaration
Model:
public class RegistrationData {
[Required(ErrorMessage = "*")]
[DisplayFormat(NullDisplayText = "First Name")]
[Display(Name = "Caption")]
public string FirstName { get; set; }
[Required(ErrorMessage = "*")]
[DisplayFormat(NullDisplayText = "Last Name")]
public string LastName { get; set; }
...
}
View:
@Html.DevExpress().FormLayout(settings => {
...
settings.Items.AddGroupItem(m => m.RegistrationData, g => {
...
g.Items.Add(m => m.RegistrationData.FirstName, i => {
i.Caption = "Name";
i.NestedExtension().TextBox(s => {
AssignTextBoxSettings(s);
s.Properties.ValidationSettings.ErrorText = "*";
});
});
g.Items.Add(m => m.RegistrationData.LastName, i => {
i.Caption = "Last Name";
i.NestedExtension().TextBox(s => {
AssignTextBoxSettings(s);
s.Properties.ValidationSettings.ErrorText = "*";
});
});
...
});
}).GetHtml()
See also: Binding Data Editors to Data
Data Display Attributes
Common data attributes supported by DevExpress data componenets are as follows.
DisplayAttribute - Specifies localizable strings for classes and members.
public class TestClass { [Display(GroupName = "{Tabs}/Contact", Order = 2), DataType(DataType.PhoneNumber)] public string Phone { get; set; } }
DisplayColumnAttribute - Specifies the column that a component (such as GridView) displays in a table as a foreign-key column.
[DisplayColumn("Phone")] public class Customer { ... }
CheckBoxValuesAttribute - Specifies the check box value.
class TestClass { [Display(Name = "Check it")] [CheckBoxValuesAttribute(AllowGrayed = false)] [CheckBoxRequired(ValidStateMode = CheckBoxValidStateMode.Checked, ErrorMessage = "Error text")] public bool CheckBoxField { get; set; } }
DisplayFormatAttribute - Specifies a data field’s display format.
public class TestClass { [DisplayFormat(DataFormatString = "MMMM/yyyy")] public DateTime Date { get; set; } }
EditableAttribute - Specifies whether you can edit a data field.
public class TestClass { [Editable(false)] public int MyInt { get; set; } }
KeyAttribute - Indicates that a property or a field is a key.
private class TestClass { [Key] public string ID { get; set; } }
MaxLengthAttribute - Specifies a property value’s maximum length.
public class TestClass { [MaxLength(5)] public string Name { get; set; } }
MinLengthAttribute - Specifies a property value’s minimum length.
public class TestClass { [MinLength(5)] public string Name { get; set; } }
PhoneAttribute - Specifies that a data field value is a phone number.
public class TestClass { [Phone] public string PhoneNumber { get; set; } }
RegularExpressionAttribute - Specifies that a data field value should match the specified regular expression.
public class TestClass { [RegularExpression(@"\S(.*)\S", ErrorMessage = "Error Text")] public object FirstName; }
UIHintAttribute - Specifies a template or user control to display a data field.
public class EditorTemplatesData { [UIHint("Mileage")] [Range(10, 999, ErrorMessage = "Must be between {1} <br /> and {2}")] public float Mileage { get; set; } }
Data Type Attributes
BindableTypeAttribute - Specifies whether the data type is bindable.
[BindableType(IsBindable = true)] class TestClass { ... }
DataTypeAttribute - Specifies the type of a data field’s value.
public class TestClass { [DataType(DataType.PhoneNumber)] public string Phone { get; set; } }
EnumDataAttribute - Replaces numeric enumerator values with corresponding declarations.
public class TestClass { [EnumDataType(typeof(TestObject))] public object TestObject { get; set; } }
MetadataTypeAttribute - Gets data annotation attributes from the specified class.
[MetadataType(typeof(EmployeeMetadata))] public partial class Employee { } public class EmployeeMetadata { [Browsable(false)] public Int32 Id { get; set; } }
ScaffoldColumnAttribute - Specifies whether a class or data column is hidden.
public class TestClass { [ScaffoldColumn(true)] public object ID; [ScaffoldColumn(false)] public object ProductName; }
ScaffoldTableAttribute - Specifies whether a class or data table is hidden.
[ScaffoldTable(true)] public class TestClass { [Display(Name = "Name")] public string Name { get; set; } ... }
TimestampAttribute - Specifies a column’s data type as a row version.
public class TestClass { public string Name { get; set; } [Timestamp] public byte[] CardID { get; set; } }
Validation Attributes
CompareAttribute - Compares an editor’s value with another property.
class TestClass { [DataType(DataType.Password), Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password), Compare("Password", ErrorMessage = "The password you entered do not match"), Display(Name = "Confirm Password")] public string ConfirmPassword { get; set; } }
ConcurrencyCheck - Checks a property for optimistic concurrency.
public class Customer { [ConcurrencyCheck] public string ContactName { get; set; } [ConcurrencyCheck] public string ContactTitle { get; set; } }
HtmlSettingsAttribute - Specifies HTML edit settings for the data field value.
public class TestClasss { [Required] [HtmlSettings(AllowScripts = false, ResourcePathMode = ResourcePathMode.RootRelative, AllowedDocumentType = AllowedDocumentType.HTML5)] public string Html { get; set; } }
CreditCardAttribute - Validates a field value as a credit card number.
public class TestClass { [CreditCard] public string CreditCardNumber { get; set; } }
CheckBoxRequiredAttribute - Specifies that a check box is required.
class TestClass { [Display(Name = "Check it")] [CheckBoxValuesAttribute(AllowGrayed = false)] [CheckBoxRequired(ValidStateMode = CheckBoxValidStateMode.Checked, ErrorMessage = "Error text")] public bool CheckBoxField { get; set; } }
CustomValidationAttribute - Specifies a custom validation method.
public class TestClass { [CustomValidation(typeof(TestValidator), "ValidateName")] public string Name { get; set; } }
EmailAddressAttribute - Validates a field value as an email address.
public class TestClass { [EmailAddress(ErrorMessage = "InvalidEmailAddress")] [Display(Name = "Email")] public string Email { get; set; } }
FileExtensionsAttribute - Validates file name extensions.
public class TestClass { [FileExtensions(Extensions=". jpg,. png,. gif")] public string FileName { get; set; } }
DateRangeAttribute - Specifies date range settings.
public class TestClass { [DateRange(StartDateEditFieldName = "TimeField", MinDayCount = 0, MaxDayCount = 3, RangeErrorText = "RangeErrorText")] public DateTime StartTime { get; set; } }
MaskAttribute - Specifies mask settings.
public class Customer { [Mask("+1 (999) 000-0000", ErrorMessage = "The phone number is not valid!")] [Required] public string Phone { get; set; } }
RangeAttribute - Specifies numeric range constraints for the data field value.
public class TestClass { [Range(1, 10, ErrorMessage = "Custom error text")] public object Length; }
StringLengthAttribute - Specifies the maximum and minimum length of characters allowed in a data field.
public class TestClass { [StringLength(4, ErrorMessage = "Error Text")] public string FirstName; [StringLength(10)] public string CompanyName; }
RequiredAttribute - Specifies that a data field value is required.
public class TestClass { [Required(ErrorMessage = "Name is required")] public string Name { get; set; } }
UrlAttribute - Validates a URL.
public class TestClass { [Url] public string NavigateUrl; }