Skip to main content

Validate and Save Data

  • 2 minutes to read

This topic explains different strategies you can use to validate user input in edit forms.

Validate Data at the Source Level

CollectionView exposes APIs to help you validate data before it is locally saved at the database level. This allows you to keep database constraints and ensure that the database update does not fail because of a bad internet connection.

Handle the ValidateAndSave event to validate input values and commit them to the data source.

The CollectionView runs validation mechanisms after a user taps the Save button in the edit form of a built-in detail form, or after you call the DetailEditFormViewModel.Save() method for a custom form. If the CollectionView does not manage to commit item changes to the data source, the ValidateItemEventArgs.IsValid property is set to false. You can also set IsValid to false manually to prevent committing source item values to the source.

The following example shows an alert message if a user did not fill out the Name and Phone text boxes on the form:

<dxcv:DXCollectionView ...
                       ValidateAndSave="collectionView_ValidateAndSave">
private void collectionView_ValidateAndSave(object sender, ValidateItemEventArgs e) {
    if (e.DataChangeType == DataChangeType.Add) {
        Contact contact = (Contact)e.Item;
        if (contact.Name == "" || contact.Phone == "") {
            ((NavigationPage)Application.Current.MainPage).CurrentPage.DisplayAlert("Alert", "Values cannot be empty.", "Cancel", "Discard");
            e.IsValid = false; 
        }
    }
}

If you want to manually commit changes to the ItemsSource, disable the ValidateItemEventArgs.AutoUpdateItemsSource property. After you commit changes to the source, call the RefreshData or RefreshItem method to make the CollectionView re-fetch source data.

void OnValidateAndSave(object sender, ValidateItemEventArgs e) {
  var item = (Issue)e.Item;
  try {
    if(e.DataChangeType == DataChangeType.Add || e.DataChangeType == DataChangeType.Edit) {
      var context = (IssuesContext)e.Context;
      context.SaveChanges();
    } else if(e.DataChangeType == DataChangeType.Delete) {
      using(var context = new IssuesContext()) {
        var issue = new Issue() { Id = item.Id };
        context.Entry(issue).State = EntityState.Deleted;
        context.SaveChanges();
      }
    }
  } catch(Exception exception) {
    e.IsValid = false;
    await AppShell.Current.DisplayAlert("Error when saving changes", exception.Message, "OK");
    return;
  }
  e.AutoUpdateItemsSource = false;
  var itemsSource = (ObservableCollection<Issue>)collectionView.ItemsSource;
  if(e.Action == DataChangeType.Edit) {
    itemsSource[e.SourceIndex] = item;
    return;
  }
  if(e.Action == DataChangeType.Add) {
    itemsSource.Add(e.Item);
    return;
  }
  if(e.Action == DataChangeType.Delete) {
    itemsSource.RemoveAt(e.SourceIndex);
    return;
  }
}

Validate Data at the DataFormView Level

Since built-in forms use DataFormView, you can use this component’s API to validate user input.

For more information, refer to the following help topic: Validate User Input in Data Form for .NET MAUI