Skip to main content
A newer version of this page is available. .

RuleRequiredFieldAttribute Class

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

Namespace: DevExpress.Persistent.Validation

Assembly: DevExpress.Persistent.Base.v19.1.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("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("Title", ref title, value); }
   }
}

The rule, specified via the RuleRequiredFieldAttribute, is considered to be broken if the target property is of the reference or nullable type, and its value is null (Nothing in VB). However, there are several exceptions:

  1. The target property is string, and its value is an empty string.
  2. The target property is of type DateTime, and its value is DateTime.MinValue
  3. The target property type implements the IEmptyCheckable interface, and IEmptyCheckable.IsEmpty is true.
  4. The target property type implements the IEnumerable interface, and the property value is an empty collection.

If any of these clauses is true, the rule is broken. 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 rule is not broken.

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("Priority", ref priority, value);
    }
}
// ...
public enum Priority {
    Unspecified,
    Low,
    Normal,
    High
}

The rule generated by the RuleRequiredFieldAttribute will be loaded to the Application Model‘s IModelValidationRules node. So, you can customize this rule via the Model Editor. 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.

By default, 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, using the IModelLayoutManagerOptionsValidation.RequiredFieldMark property.

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 19.1\Components\eXpressApp Framework\FeatureCenter folder, by default.

Inheritance

Object
Attribute
RuleBaseAttribute
RuleRequiredFieldAttribute
See Also