Skip to main content
.NET 6.0+

RuleFromBoolPropertyAttribute Class

Defines a validation rule that demands a true value for the target public non-persistent Boolean property.

Namespace: DevExpress.Persistent.Validation

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

Declaration

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class RuleFromBoolPropertyAttribute :
    RuleBaseAttribute,
    IRuleFromBoolPropertyProperties,
    IRulePropertyValueProperties,
    IRuleCollectionPropertyProperties,
    IRuleBaseProperties

Remarks

Apply this attribute to a business class’ Boolean public non-persistent property to define a rule that will be satisfied when this property returns true. This rule is not intended for checking the target property’s data, and therefore, this property should be non-persistent. Implement a complex check in this property, and apply the RuleFromBoolPropertyAttribute to it. Use this approach (a non-persistent Boolean property plus the RuleFromBoolPropertyAttribute) when none of the remaining built-in validation rules suit your needs. To provide details on the rule, use the common parameters that are inherited from the RuleBaseAttribute class.

The rule generated by the RuleFromBoolPropertyAttribute will be loaded to the Application Model‘s IModelValidationRules node. So, you can customize this rule via the Model Editor. For details, refer to the Implement Property Value Validation topic.

Note

The property to which the RuleFromBoolProperty attribute is applied must be public. If it is necessary to hide the property from the UI, decorate it with the Browsable(false) attribute. This may also improve the performance, because the property getter will be invoked only when required.

The following code demonstrates how to use the RuleFromBoolPropertyAttribute. The IsEmailUnique property checks whether the current Contact object’s Email is not set for other Contact objects. The rule generated from the attribute is checked when committing a Contact object to the database. When the IsEmailUnique property returns false, an error message with the provided details is invoked.

using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Persistent.Validation;
using System.ComponentModel.DataAnnotations;
// ...
public class Contact : BaseObject {
   public virtual string Email { get; set; }
   [Browsable(false)]
   [RuleFromBoolProperty("ContactUniqueEmail", DefaultContexts.Save, 
      "Contact with this Email already exists")]
   public bool IsEmailUnique {
       get {
           string toLowerEmail = Object.ReferenceEquals(Email, null) ? String.Empty : Email.ToLower();
           return !ObjectSpace.GetObjectsQuery<Employee>(true)
               .Any(t => t.Email.ToLower() == toLowerEmail && t.ID != this.ID);
       }
   }
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

You can highlight the property that should be corrected for the current object’s validity. In the instance above, you can highlight the Email property as an invalid property. To highlight the required properties, pass their names as the RuleFromBoolPropertyAttribute.UsedProperties parameter (use a comma to separate multiple property names).

[RuleFromBoolProperty("ContactUniqueEmail", DefaultContexts.Save, 
  "Contact with this Email already exists", UsedProperties = "Email")]
public virtual bool IsEmailUnique {
      //...
}

Important

The RuleFromBoolPropertyAttribute attribute’s SkipNullOrEmptyValues property is set to true. To run rules when used properties are null or empty, set SkipNullOrEmptyValues to false.

You can see examples of the RuleFromBoolProperty and other validation attributes in the Validation section of the FeatureCenter demo. This demo is located in the %PUBLIC%\Documents\DevExpress Demos 23.2\Components\XAF\FeatureCenter.NETFramework.XPO folder, by default.

Inheritance

Object
Attribute
RuleBaseAttribute
RuleFromBoolPropertyAttribute
See Also