Back to Devexpress

DataFormView.ValidateProperty Event

maui-devexpress-dot-maui-dot-dataform-dot-dataformview-887f380c.md

latest4.0 KB
Original Source

DataFormView.ValidateProperty Event

Allows you to validate a specific data editor on the form.

Namespace : DevExpress.Maui.DataForm

Assembly : DevExpress.Maui.Editors.dll

NuGet Package : DevExpress.Maui.Editors

Declaration

csharp
public event DataFormPropertyValidationEventHandler ValidateProperty

Event Data

The ValidateProperty event's data class is DataFormPropertyValidationEventArgs. The following properties provide information specific to this event:

PropertyDescription
CurrentValueGets or sets the current value of the data field that is about to be updated.
ErrorTextGets or sets the error message displayed below the editor on the data form.
HasErrorGets or sets whether the new value is valid.
NewValueGets the new value that should be validated.
PropertyNameGets the name of the processed data field (property).

Remarks

You can handle the following events to validate user input:

  • ValidateProperty—allows you to validate a specific field on the form. Use the PropertyName event argument to determine the processed data field. The CurrentValue and NewValue arguments specify the current data field value and the value that should be validated. The DataFormView raises the ValidateProperty event only if the CurrentValue and NewValue are different. If the new value is not valid, set the HasError argument to true and use the ErrorText argument to notify the user about the error.

  • ValidateForm—allows you to validate all data fields on the form. This event stores new values and error messages in dictionaries, where keys are data field names.

See the following topic for more information: Validate User Input in Data Form for .NET MAUI.

Example

The code below shows how to ensure that a password contains at least 8 characters and includes uppercase and lowercase characters, numbers, and special characters.

csharp
using DevExpress.Maui.DataForm;
// ... 

public partial class MainPage : ContentPage {
    public MainPage() {
        InitializeComponent();
        dataForm.DataObject = new PersonalInfo();
        dataForm.ValidateProperty += dataForm_ValidateProperty;
    }
    private void dataForm_ValidateProperty(object sender, DataFormPropertyValidationEventArgs e) {
        if (e.PropertyName == "Password") {
            var regex = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");
            if (!regex.IsMatch(e.NewValue.ToString())) {
                e.HasError = true;
                e.ErrorText = "The password must contain at least 8 characters " +
                    "including uppercase, lowercase, numbers, and special characters.";
            }
        }
    }
}

See Also

DataFormView Class

DataFormView Members

DevExpress.Maui.DataForm Namespace