Skip to main content

Lesson 4: Validate and Commit

  • 3 minutes to read

DataFormView supports several modes to validate and commit values that users enter in data form editors. This document lists API members related to validation and commit, and describes ways to validate data values based on the sample data form (the Password value) from Lesson 3.

Correct Password

Detected Error

Validate

The example below enables the data form to validate the user input when an editor loses focus.

<dxdf:DataFormView x:Name="dataForm" 
                   ValidationMode="LostFocus">
    <!-- ... -->
</dxdf:DataFormView>

DataFormView allows you to validate input values in the following ways.

ValidateProperty Event

Handle the ValidateProperty event:

  1. Specify validation rules
    Use the event parameter’s CurrentValue and NewValue properties to get the editor’s original and new value (the value that should be validated). The PropertyName property allows you to obtain which property value is being edited.
  2. Indicate an error
    If the input value fails validation, set the HasError property to true and ErrorText to the error description.
using DevExpress.XamarinForms.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") {
            if (e.NewValue.ToString().Length < 6) {
                e.HasError = true;
                e.ErrorText = "The password should contain more than 5 characters.";
            }
        }
    }
}

Data Annotations

To use DataAnnotations attributes, add the System.ComponentModel.Annotations NuGet package to your project.

using System.ComponentModel.DataAnnotations;
// ... 

namespace DataFormExample {
    public class PersonalInfo {

        [StringLength(50, MinimumLength = 6, 
                          ErrorMessage = "The password should contain more than 5 characters.")]
        public string Password { get; set; }

        // ... 
    }
}

INotifyDataErrorInfo and IDataErrorInfo Interfaces

Implement the INotifyDataErrorInfo or IDataErrorInfo interface in the data object’s class.

Note

Use this approach to validate a value that was already committed to the data object.

using System.ComponentModel;
// ... 

namespace DataFormExample {
    public class PersonalInfo : IDataErrorInfo{
        string IDataErrorInfo.this[string columnName]{
            get {
                if (columnName == "Password" && this.Password.Length < 6) {
                    return "The password should contain more than 5 characters.";
                }
                return null;
            }
        } 
        //...
        string IDataErrorInfo.Error => throw new NotImplementedException();
    }
}

Commit

The DataFormView.CommitMode property specifies when input values should be committed to the underlying data object. The following modes are available:

  • PropertyChanged - a value is committed after changes.
  • LostFocus - a value is committed when the editor loses focus (a user taps outside the editor or presses Enter on a device’s keyboard).
  • Manually - a value is committed when the Commit method is called.

Note

Each commit method calls validation first. A property value is only committed if it passes validation.

<dxdf:DataFormView x:Name="dataForm" 
                   CommitMode="PropertyChanged">
    <!-- ... -->
</dxdf:DataFormView>