DXValidationProvider Class
Provides data validation management for DevExpress bound and unbound editors.
Namespace: DevExpress.XtraEditors.DXErrorProvider
Assembly: DevExpress.XtraEditors.v19.2.dll
Declaration
[ToolboxBitmap(typeof(ToolboxIconsRootNS), "DXValidationProvider")]
public class DXValidationProvider :
Component,
IExtenderProvider,
ISupportInitialize
Remarks
The DXValidationProvider component allows you to create various validation rules and associate them with editors derived from the BaseEdit class. With the DXValidationProvider you don’t need to write validation code.
There are two built-in validation rules:
CompareAgainstControlValidationRule
Compares the values of two BaseEdit editors - and determines whether one is less than, equal to or greater than the other.
ConditionValidationRule
Checks whether a BaseEdit 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).
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.
To create validation rules at design time and associate them with editors, use the ‘Customize Validation Rules’ editor:
If a user enters an invalid value, the editor displays a notification.
By default, automatic data checking is disabled. To validate the data of a specific editor or all the editors associated with the DXValidationProvider, use the DXValidationProvider.Validate method. To enable automatic data checking, set the DXValidationProvider.ValidationMode property to ‘Auto’.
Editors whose values are invalid can be accessed via the DXValidationProvider.InvalidControls property.
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;
// Validate editors manually (when the OK button is clicked)
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();
}
// 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;
}
}