Back to Devexpress

DXValidationProvider Class

windowsforms-devexpress-dot-xtraeditors-dot-dxerrorprovider-1316524a.md

latest8.9 KB
Original Source

DXValidationProvider Class

Provides data validation management for DevExpress bound and unbound editors.

Namespace : DevExpress.XtraEditors.DXErrorProvider

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXLicenseWinFormsEditors]
public class DXValidationProvider :
    ComponentBase,
    IExtenderProvider,
    ISupportInitialize
vb
<DXLicenseWinFormsEditors>
Public Class DXValidationProvider
    Inherits ComponentBase
    Implements IExtenderProvider,
               ISupportInitialize

Remarks

The DXValidationProvider component allows you to create various validation rules and associate them with editors (BaseEdit descendants). With the DXValidationProvider you don’t need to write validation code.

There are two built-in validation rules:

  • CompareAgainstControlValidationRule

  • ConditionValidationRule

You can also implement a custom validation rule. Create a ValidationRule descendant and override the Validate method (see the example below).

To associate a validation rule with an editor, use the DXValidationProvider.SetValidationRule method. The DXValidationProvider.GetValidationRule method returns a validation rule associated with the specified editor.

Create Validation Rules at Design-Time

Use the Customize Validation Rules editor to create validation rules at design time and associate them with editors.

Data Validation Modes

By default, automatic data validation is disabled. Use the DXValidationProvider.Validate method to validate data editors.

Set the DXValidationProvider.ValidationMode property to ‘Auto’ to enable automatic data validation.

Error Indication

The editor displays a notification icon if a user enters an invalid value.

The notification icon is hidden once the user enters a valid value and moves the focus to another control or presses Enter (ValidateOnEnterKey).

Clear Validation Errors

Use the SetValidationRule(Control, ValidationRuleBase) method and pass null ( Nothing in Visual Basic) as a rule parameter to clear the validation rule for the specified control.

Access Editors with Invalid Values

Use the DXValidationProvider.GetInvalidControls method to access data editors with invalid values.

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.”.

csharp
using DevExpress.XtraEditors.DXErrorProvider;

// Validates the editors when a user presses/clicks the OK button.
dxValidationProvider1.ValidationMode = ValidationMode.Manual;

// ...
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);

private void buttonOk_Click(object sender, EventArgs e) {
    dxValidationProvider1.Validate();
}

// Implements 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." };
        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) return true;
            }
        }
        return false;
    }
}
vb
Imports DevExpress.XtraEditors.DXErrorProvider

' Validates the editors when a user presses/clicks the OK button.
dxValidationProvider1.ValidationMode = ValidationMode.Manual
' ...
Dim containsValidationRule As New ConditionValidationRule()
containsValidationRule.ConditionOperator = ConditionOperator.Contains
containsValidationRule.Value1 = "@"
containsValidationRule.ErrorText = "Please enter a valid email"
containsValidationRule.ErrorType = ErrorType.Warning

Dim compValidationRule As 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

Dim customValidationRule As 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)

Private Sub ButtonOK_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
    dxValidationProvider1.Validate()
End Sub

' Implements a custom validation rule.
Public Class CustomValidationRule
    Inherits ValidationRule

    Public Overrides Function Validate(ByVal control As Control, ByVal value As Object) As Boolean
        Dim str As String = DirectCast(value, String)
        Dim values() As String = { "Dr.", "Mr.", "Mrs.", "Miss", "Ms." }
        For Each val As String In values
            If ValidationHelper.Validate(str, ConditionOperator.BeginsWith, val, Nothing, Nothing, False) Then
                Dim name As String = str.Substring(val.Length)
                If name.Trim().Length > 0 Then
                    Return True
                End If
            End If
        Next val
        Return False
    End Function
End Class

Inheritance

Object MarshalByRefObject Component DevExpress.XtraEditors.ComponentBase DXValidationProvider

See Also

DXValidationProvider Members

DevExpress.XtraEditors.DXErrorProvider Namespace