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

DXValidationProvider.SetValidationRule(Control, ValidationRuleBase) Method

Associates a validation rule with the specified BaseEdit descendant.

Namespace: DevExpress.XtraEditors.DXErrorProvider

Assembly: DevExpress.XtraEditors.v18.2.dll

Declaration

[DXCategory("Data")]
[DefaultValue(null)]
public virtual void SetValidationRule(
    Control control,
    ValidationRuleBase rule
)

Parameters

Name Type Description
control Control

A BaseEdit descendant that represents the editor.

rule DevExpress.XtraEditors.DXErrorProvider.ValidationRuleBase

A DevExpress.XtraEditors.DXErrorProvider.ValidationRuleBase descendant that represents the validation rule.

Remarks

The DXValidationProvider has two built-in validation rules:

  • CompareAgainstControlValidationRule

    Compares the values of two editors – determines whether one is less than, equal to or greater than the other.

  • ConditionValidationRule

    Checks whether an editor’s value matches the specified condition (‘BeginsWith’, ‘Between’, ‘IsBlank’, etc.).

It’s also possible to implement a custom validation rule. To do this, create a ValidationRule descendant and override the Validate method (see the example).

Example

This example demonstrates how to validate data via the DXValidationProvider component. In this example, validation rules (built-in and custom) are created in code, and associated with editors using the DXValidationProvider.SetValidationRule method.

The CustomValidationRule class represents a custom validation rule that checks whether the editor’s value begins with “Dr.”, “Mr.”, “Mrs.”, “Miss” or “Ms.”.

using DevExpress.XtraEditors.DXErrorProvider;

// ...
ConditionValidationRule containsValidationRule = new ConditionValidationRule();
containsValidationRule.ConditionOperator = ConditionOperator.Contains;
containsValidationRule.Value1 = '@';
containsValidationRule.ErrorText = "Please enter a valid email";
containsValidationRule.ErrorType = ErrorType.Warning;

CompareAgainstControlValidationRule compValidationRule = 
    new CompareAgainstControlValidationRule();
compValidationRule.Control = notEmptyTextEdit;
compValidationRule.CompareControlOperator = CompareControlOperator.Equals;
compValidationRule.ErrorText = "Please enter a value that equals to the first editor's value";
compValidationRule.CaseSensitive = true;

CustomValidationRule customValidationRule = new CustomValidationRule();
customValidationRule.ErrorText = "Please enter a valid person name";
customValidationRule.ErrorType = ErrorType.Warning;

dxValidationProvider1.SetValidationRule(containsTextEdit, containsValidationRule);
dxValidationProvider1.SetValidationRule(compareTextEdit, compValidationRule);
dxValidationProvider1.SetValidationRule(customTextEdit, customValidationRule);

// Represents a custom validation rule.
public class CustomValidationRule : ValidationRule {
    public override bool Validate(Control control, object value) {
        string str = (string)value;
        string[] values = new string[] { "Dr.", "Mr.", "Mrs.", "Miss", "Ms." };
        bool res = false;
        foreach(string val in values) {
            if(ValidationHelper.Validate(str, ConditionOperator.BeginsWith, 
                val, null, null, false)) {
                string name = str.Substring(val.Length);
                if(name.Trim().Length > 0) res = true;
            }
        }
        return res;
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the SetValidationRule(Control, ValidationRuleBase) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also