Skip to main content
.NET Framework 4.6.2+

RuleRequiredFieldAttribute Class

Defines a validation rule that demands that a property has a value.

Namespace: DevExpress.Persistent.Validation

Assembly: DevExpress.Persistent.Base.v24.2.dll

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 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.

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
XAF ASP.NET Core Blazor Required Field Mark, DevExpress
Windows Forms
XAF Windows Forms Required Field Mark, DevExpress
ASP.NET Web Forms
XAF ASP.NET Web Forms Required Field Mark, DevExpress

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.2\Components\XAF\FeatureCenter.NETFramework.XPO folder by default.

Inheritance

Object
Attribute
RuleBaseAttribute
RuleRequiredFieldAttribute
See Also