expressappframework-devexpress-dot-persistent-dot-validation-e9b6b0ed.md
Defines a validation rule that demands that a property has a value.
Namespace : DevExpress.Persistent.Validation
Assembly : DevExpress.Persistent.Base.v25.2.dll
NuGet Package : DevExpress.Persistent.Base
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class RuleRequiredFieldAttribute :
RuleBaseAttribute,
IRuleRequiredFieldProperties,
IRulePropertyValueProperties,
IRuleCollectionPropertyProperties,
IRuleBaseProperties
<AttributeUsage(AttributeTargets.Property, AllowMultiple:=True)>
Public Class RuleRequiredFieldAttribute
Inherits RuleBaseAttribute
Implements IRuleRequiredFieldProperties,
IRulePropertyValueProperties,
IRuleCollectionPropertyProperties,
IRuleBaseProperties
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.
[DefaultClassOptions]
[System.ComponentModel.DefaultProperty(nameof(Title))]
public class Position : BaseObject {
//...
private string title;
[RuleRequiredField("RuleRequiredField for Position.Title",DefaultContexts.Save,
"A title must be specified")]
public string Title {
get { return title; }
set { SetPropertyValue(nameof(Title), ref title, value); }
}
}
When a property is decorated with a RuleRequiredFieldAttribute, the property value is not valid in the following cases:
null.DateTime.MinValuetrue.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 ValueConverterAttribute, ValueConverter is ignored during validation. For example, if the database value is null but the actual value is not, the property value is valid.
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.
[RuleValueComparison(
null, DefaultContexts.Save, ValueComparisonType.NotEquals, Priority.Unspecified)]
public Priority Priority {
get { return priority; }
set {
SetPropertyValue(nameof(Priority), ref priority, value);
}
}
// ...
public enum Priority {
Unspecified,
Low,
Normal,
High
}
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.
XAF appends an asterisk symbol (*) to an editor label if the corresponding field value is required.
ASP.NET Core Blazor Windows Forms
You can customize the appended string in the Model Editor. Use the IModelLayoutManagerOptionsValidation.RequiredFieldMark property to specify a custom string.
Tip
You can find examples of this functionality in the MainDemo demo included with XAF. The demo illustrates various XAF features (including RuleRequiredField and other validation attributes) and is located in the %PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\MainDemo.NET.EFCore folder.
IRuleCollectionPropertyProperties
Object Attribute RuleBaseAttribute RuleRequiredFieldAttribute
See Also