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

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.v18.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 in the Application Model 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.Data.Filtering;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
// ...
public class Contact: BaseObject {
   //... 
   private string email;
   public string Email {
       get { return email; }
       set { SetPropertyValue("Email", ref email, value); }
   }
   [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();
         XPCollection<Contact> contacts = 
               new XPCollection<Contact>(PersistentCriteriaEvaluationBehavior.InTransaction,
               Session, new BinaryOperator(new FunctionOperator(FunctionOperatorType.Lower, 
               new OperandProperty("Email")),
            new OperandValue(toLowerEmail), BinaryOperatorType.Equal));
         foreach (Contact contact in contacts) {
            if (contact != this) {
               return false;
            }
         }
         return true;
      }
   }
}

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 bool IsEmailUnique {
      //...
}

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

Inheritance

Object
Attribute
RuleBaseAttribute
RuleFromBoolPropertyAttribute
See Also