Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

RuleRequiredFieldAttribute Class

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

Namespace: DevExpress.Persistent.Validation

Assembly: DevExpress.Persistent.Base.v20.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 {
   //...
   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 the RuleRequiredFieldAttribute attribute, 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, the ValueConverters are not considered when validating. For instance, if the database stored 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, the RuleRequiredFieldAttribute is appropriate when it is required to ensure the property value is specified. However, in some scenarios, the RuleCriteriaAttribute or RuleValueComparisonAttribute should be used. For instance, the RuleRequiredFieldAttribute will not validate the enumerations, as they are value types. You can use the following approach when validating the enumeration property value:

[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
}

Customize the Validation Rule

The rule generated by the 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 which is implemented in a base class provided by a third party library. For details, refer to the Implement Property Value Validation in the Application Model topic.

Required Field Mark

An asterisk symbol (*) is appended to an editor label, when the field value is required.

RequiredFieldMark

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 located in the %PUBLIC%\Documents\DevExpress Demos 20.2\Components.NET Core Desktop Libraries\eXpressApp Framework\FeatureCenter folder, by default.

Inheritance

Object
Attribute
RuleBaseAttribute
RuleRequiredFieldAttribute
See Also