Back to Devexpress

RuleRangeAttribute Class

expressappframework-devexpress-dot-persistent-dot-validation-cac617ce.md

latest5.2 KB
Original Source

RuleRangeAttribute Class

Defines a validation rule that demands the target property’s value be within the specified value range (including the range’s end points).

Namespace : DevExpress.Persistent.Validation

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

NuGet Package : DevExpress.Persistent.Base

Declaration

csharp
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class RuleRangeAttribute :
    RuleBaseAttribute,
    IRuleRangeProperties,
    IRulePropertyValueProperties,
    IRuleCollectionPropertyProperties,
    IRuleBaseProperties,
    IRuleSupportsCollectionAggregatesProperties
vb
<AttributeUsage(AttributeTargets.Property, AllowMultiple:=True)>
Public Class RuleRangeAttribute
    Inherits RuleBaseAttribute
    Implements IRuleRangeProperties,
               IRulePropertyValueProperties,
               IRuleCollectionPropertyProperties,
               IRuleBaseProperties,
               IRuleSupportsCollectionAggregatesProperties

Remarks

Apply this attribute to a business class property to define a validation rule that will check whether this property’s value is within the specified range. Use the minimalValue and maximumValue parameters to specify the start and end points of the target property value’s range. These parameter values must be the same type as the property or a value type that implements the IComparable interface; otherwise, an ArgumentException is thrown.

You can use Function Criteria Operators as the minimalValue and maximumValue parameter values. For instance, you can pass the LocalDateTimeLastWeek() and LocalDateTimeToday parameters to check whether the target property’s value is set to a date from last week.

When applying this attribute, use common parameters that are inherited from the RuleBaseAttribute class, in addition to the specific parameters.

csharp
using System;
using DevExpress.Persistent.Validation;
//...
[RuleRange("LastSevenDays_RuleRange", "Save", "AddDays(LocalDateTimeToday(), -7)", 
    "LocalDateTimeToday()", ParametersMode.Expression)]
public virtual DateTime LastSevenDays { get; set; }
//...
// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
csharp
using System;
using DevExpress.Persistent.Validation;
//...
private DateTime lastSevenDays;
[RuleRange("LastSevenDays_RuleRange", "Save", "AddDays(LocalDateTimeToday(), -7)", 
    "LocalDateTimeToday()", ParametersMode.Expression)]
public DateTime LastSevenDays {
    get { return lastSevenDays; }
    set { SetPropertyValue(nameof(LastSevenDays), ref lastSevenDays, value); }
}
//...

The rule generated by the RuleRangeAttribute will be loaded to the Application Model‘s IModelValidationRules node. You can customize this rule with the Model Editor. You can also create new rules in the Model Editor. This is helpful when you need to add a rule to a property that is implemented in a base class supplied by a third party library. Refer to the following topic for details: Implement Property Value Validation.

Tip

You can find examples of this functionality in the MainDemo demo included with XAF. The demo illustrates various XAF features (including RuleRange attribute) and is located in the %PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\MainDemo.NET.EFCore folder.

Implements

IRuleSupportsCollectionAggregatesProperties

IRuleCollectionPropertyProperties

IRuleBaseProperties

Inheritance

Object Attribute RuleBaseAttribute RuleRangeAttribute

See Also

RuleRangeAttribute Members

Validation Rules

DevExpress.Persistent.Validation Namespace