RuleRequiredFieldAttribute Class
Defines a validation rule that demands that a property has a value.
Namespace: DevExpress.Persistent.Validation
Assembly: DevExpress.Persistent.Base.v24.1.dll
NuGet Package: DevExpress.Persistent.Base
#Declaration
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class RuleRequiredFieldAttribute :
RuleBaseAttribute,
IRuleRequiredFieldProperties,
IRulePropertyValueProperties,
IRuleCollectionPropertyProperties,
IRuleBaseProperties
#Remarks
Apply this attribute to a property to define a validation rule that will check whether this property value is specified. Use the common parameters that are inherited from the RuleBaseAttribute class.
[DefaultClassOptions]
[System.ComponentModel.DefaultProperty(nameof(Title))]
public class Position : BaseObject {
[RuleRequiredField("RuleRequiredField for Position.Title",DefaultContexts.Save,
"A title must be specified")]
public virtual string Title { get; set; }
}
// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
When a property is decorated with a RuleRequiredFieldAttribute
, the property value is not valid in the following cases:
- The property value is
null
(Nothing
in VB). - The property is string, and its value is an empty string.
- The property is of type DateTime, and its value is
DateTime.MinValue
- The property type implements the IEmptyCheckable interface, and IEmptyCheckable.IsEmpty is
true
. - The property type implements the IEnumerable interface, and the property value is an empty collection.
If any of these clauses is true, the property value is not valid. The clause priority corresponds to the order in the list above. You can handle the RuleSet.CustomIsEmptyValue event to override this behavior.
Note
If the target property is decorated with the Valuenull
but the actual value is not, the property value is valid.
#Requirements
XAF requires that the properties decorated with the RuleRequiredFieldAttribute
attribute must be of nullable or reference type. Otherwise, the validation rule does not work.
In most cases, RuleRequiredFieldAttribute
is used when you need to ensure that a property has a value. However, some scenarios require RuleCriteriaAttribute or RuleValueComparisonAttribute. For instance, RuleRequiredFieldAttribute
does not validate enumerations, as they are value types. You can use the following technique to validate an enumeration property value:
[RuleValueComparison(
null, DefaultContexts.Save, ValueComparisonType.NotEquals, Priority.Unspecified)]
public virtual Priority Priority { get; set; }
// ...
public enum Priority {
Unspecified,
Low,
Normal,
High
}
// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
#Customize the Validation Rule
The rule generated by RuleRequiredFieldAttribute
is loaded to the Application Model‘s IModelValidationRules node. You can use the Model Editor to customize this rule.
You can create new rules in the Model Editor as well. This is helpful when you need to add a rule to a property that is implemented in a base class from a third-party library.
For more information, refer to the following topic: Implement Property Value Validation.
#Required Field Mark
XAF appends an asterisk symbol (*) to an editor label if the corresponding field value is required.
- ASP.NET Core Blazor
- Windows Forms
- ASP.NET Web Forms
You can customize the appended string in the Model Editor. Use the IModelLayoutManagerOptionsValidation.RequiredFieldMark property to specify a custom string.
#Examples
You can see examples of using the RuleRequiredField
and other validation attributes in the Validation section of the FeatureCenter demo. This demo is in the %PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF\FeatureCenter.NETFramework.XPO folder by default.