Back to Devexpress

RuleRegularExpressionAttribute Class

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

latest4.5 KB
Original Source

RuleRegularExpressionAttribute Class

Defines a validation rule that demands the target property match a specified pattern.

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 RuleRegularExpressionAttribute :
    RuleBaseAttribute,
    IRuleRegularExpressionProperties,
    IRulePropertyValueProperties,
    IRuleCollectionPropertyProperties,
    IRuleBaseProperties
vb
<AttributeUsage(AttributeTargets.Property, AllowMultiple:=True)>
Public Class RuleRegularExpressionAttribute
    Inherits RuleBaseAttribute
    Implements IRuleRegularExpressionProperties,
               IRulePropertyValueProperties,
               IRuleCollectionPropertyProperties,
               IRuleBaseProperties

Remarks

Apply this attribute to a business class string property to define a validation rule that will check whether this property’s value matches a specified regular expression. Use the pattern parameter to specify the regular expression that the property must match. In addition, use the common parameters that are inherited from the RuleBaseAttribute class.

csharp
[RuleRegularExpression("",DefaultContexts.Save,
    @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")]
public virtual string Email { get; set; }

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
csharp
//...
private string email;
[RuleRegularExpression("",DefaultContexts.Save,
    @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")]
public string Email {
   get { return email; }
   set { SetPropertyValue(nameof(Email), ref email, value); }
}
//...

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

Tip

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

Implements

IRuleCollectionPropertyProperties

IRuleBaseProperties

Inheritance

Object Attribute RuleBaseAttribute RuleRegularExpressionAttribute

See Also

RuleRegularExpressionAttribute Members

Validation Rules

DevExpress.Persistent.Validation Namespace