Input Validation
- 2 minutes to read
#Validation Modes and Behavior
Each editor provides two properties that specify when validation should take place. The BaseEdit.ValidateOnTextInput property specifies whether or not the editor's value should be validated while typing within its text box. The BaseEdit.ValidateOnEnterKeyPressed property specifies whether or not the value should be validated when pressing the ENTER key.
To manually force the validation, call the BaseEdit.DoValidate method.
NOTE
To disable data validation, set the Base
#Implementing Validation Rules
Each time the editor's value should be validated, the editor fires the BaseEdit.Validate event, allowing you to implement validation rules (e.g. to limit the range of valid values, implement a custom conditional validation mechanism, etc.).
The editor's value is returned by the event parameter's ValidationEventArgs.Value property. This property is set to the last changed property value (e.g. for TextEdit, these are the TextEditBase.Text and BaseEdit.EditValue properties). If the editor's value has not been changed, the ValidationEventArgs.Value property returns a null reference.
If the ValidationEventArgs.Value property's value is invalid, set the ValidationEventArgs.IsValid property to false. In this instance, the editor displays an animated error icon and corresponding tooltip showing the error message. Use the event parameter's BaseValidationError.ErrorContent and BaseValidationError.ErrorType properties to specify the text displayed within the error tooltip and error icon respectively.
To specify the editor's response to an invalid value, use the BaseEdit.InvalidValueBehavior property. Setting this property to InvalidValueBehavior.WaitForValidValue blocks focus transition to other controls until a valid value is entered. To allow an end-user to switch to another editor and re-enter a value for the editor (with the invalid value) later, set the BaseEdit.InvalidValueBehavior property to InvalidValueBehavior.AllowLeaveEditor.
#Example
The following example shows how to handle the BaseEdit.Validate event to provide a custom validation procedure.
The image below shows the result:
private void agTextEdit_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e) {
if (e.Value == null) return;
if (e.Value.ToString().Length > 4) return;
e.IsValid = false;
e.ErrorType = DevExpress.Xpf.Editors.Internal.ErrorType.Information;
e.ErrorContent = "User ID is less than five symbols. Please correct.";
}