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.v24.2.dll
NuGet Package: DevExpress.Persistent.Base
#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 Rule
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 Rule
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 24.2\Components\XAF\FeatureCenter.NETFramework.XPO folder, by default.