API Reference: RequiredAttribute Class
This attribute is part of the .NET System.ComponentModel.DataAnnotations namespace.
The RequiredAttribute
can be applied to content model properties. The attribute specifies that this is a required property. The back-office user interface will not accept empty values and display a validation message.
If RequiredAttribute
is not specified on a property, the property is not required if the type is nullable. This includes classes, strings, and Nullable<T>
types. If the type is not nullable then the property is automatically required by convention. You can still then use the RequiredAttribute
to change the validation message.
Usage
Decorate the property class with the RequiredAttribute
. Optionally pass a validation message in the constructor which will be displayed when the user tries to save content while the value of this property is empty. If you do not set a custom validation message, a default culture-specific validation message is displayed.
Example
public class SampleContent: IContentModel
{
// Nullable types need to be specified as required explicitly.
[Required(ErrorMessage = "This is a required property.")]
public string RequiredText { get; set; }
// Non-nullable types are always required by convention.
public int Count { get; set; }
}