Skip to main content
All docs
V24.2

Validate User Input in Blazor TreeList

  • 11 minutes to read

The TreeList allows you to validate user input and display validation results.

Run Demo: Input Validation

Important

You should not rely on TreeList input validation alone to secure your Blazor-powered app. TreeList validation is designed to improve usability. A threat actor can bypass validation and send malicious data to the server. To minimize security related threats/risks, you must validate user input using multiple strategies. Refer to the following topic for more information: Validate User Input.

Built-In Validation

The TreeList component uses a DataAnnotationsValidator to validate user input based on data annotation attributes defined in the edit model. The TreeList validates editor values in the following cases:

  • A user modifies a data editor’s value and removes focus from this editor.
  • A user attempts to save changes.
  • You call the SaveChangesAsync() method.

In EditCell mode, the TreeList component also validates cell values when focus leaves the edited row. The component prevents users from editing another row until they address all validation errors.

Based on the edit mode, the TreeList component displays validation results in one of the following ways:

Validation in Cell Editors

In EditRow and EditCell edit modes, the TreeList component displays error validation icons in cell editors. Users can hover the mouse pointer over an icon to display the corresponding error message in the tooltip.

Blazor TreeList Validation in Cell Editors

Enable an editor’s ShowValidationSuccessState property to display a success icon when validation passes. The GlobalOptions.ShowValidationSuccessState setting allows you to enable success validation icons for all editors inside and outside the TreeList component.

The GlobalOptions.ShowValidationIcon property does not affect editors displayed in TreeList cells. To hide validation icons in a cell editor, set its ShowValidationIcon property to false.

Validation in Edit Forms

In EditForm and PopupEditForm edit modes, the TreeList component uses the standard form validation technique. If validation fails, the TreeList component marks editors with red outlines and displays error validation icons. Users can hover the mouse pointer over an icon to display the corresponding error message in the tooltip.

Enable the editor’s ShowValidationSuccessState property to display green outline and validation icon after successful validation:

Blazor TreeList Validation in Edit Form Editors

@inject EmployeeTaskService EmployeeTaskService

<DxTreeList Data="TreeListData"
            KeyFieldName="Id"
            ParentKeyFieldName="ParentId"
            EditMode="TreeListEditMode.EditForm"
            CustomizeEditModel="TreeList_CustomizeEditModel"
            EditModelSaving="TreeList_EditModelSaving"
            DataItemDeleting="TreeList_DataItemDeleting"
            CustomizeDataRowEditor="TreeList_CustomizeDataRowEditor">
    <Columns>
        <DxTreeListCommandColumn />
        <DxTreeListDataColumn FieldName="Name" Caption="Task" />
        <DxTreeListDataColumn FieldName="EmployeeName" />
        <DxTreeListDataColumn FieldName="StartDate" />
        <DxTreeListDataColumn FieldName="DueDate" />
    </Columns>
    <EditFormTemplate Context="EditFormContext">
        <DxFormLayout CssClass="w-100">
            <DxFormLayoutItem Caption="Task:" ColSpanMd="6">
                @EditFormContext.GetEditor("Name")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Employee Name:" ColSpanMd="6">
                @EditFormContext.GetEditor("EmployeeName")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Start Date:" ColSpanMd="6">
                @EditFormContext.GetEditor("StartDate")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Due Date:" ColSpanMd="6">
                @EditFormContext.GetEditor("DueDate")
            </DxFormLayoutItem>
        </DxFormLayout>
    </EditFormTemplate>
</DxTreeList>

@code {
    List<EmployeeTask> TreeListData { get; set; }

    protected override void OnInitialized() {
        TreeListData = EmployeeTaskService.GenerateData();
    }
    void TreeList_CustomizeDataRowEditor(TreeListCustomizeDataRowEditorEventArgs e) {
        var settings = (IEditSettings)e.EditSettings;
        settings.ShowValidationSuccessState = true;
    }
    void TreeList_CustomizeEditModel(TreeListCustomizeEditModelEventArgs e) {
        if (e.IsNew) {
            var newTask = (EmployeeTask)e.EditModel;
            newTask.Id = TreeListData.Max(x => x.Id) + 1;
            if (e.ParentDataItem != null)
                newTask.ParentId = ((EmployeeTask)e.ParentDataItem).Id;
        }
    }
    async Task TreeList_EditModelSaving(TreeListEditModelSavingEventArgs e) {
        if (e.IsNew)
            TreeListData.Add((EmployeeTask)e.EditModel);
        else
            e.CopyChangesToDataItem();
    }
    async Task TreeList_DataItemDeleting(TreeListDataItemDeletingEventArgs e) {
        TreeListData.Remove((EmployeeTask)e.DataItem);
    }
}

Disable an editor’s ShowValidationIcon property to hide validation icons in this editor. Specify GlobalOptions.ShowValidationIcon and GlobalOptions.ShowValidationSuccessState properties to customize validation results for editors in the TreeList edit form and standard EditForm.

Display Validation Message

When you define EditFormTemplate or CellEditTemplate, you can use any of the following techniques to display validation messages:

  • Use Blazor’s standard ValidationMessage component to display messages for individual data editors.
  • Use the template context’s EditContext property to access validation messages and display them manually.
  • Use the ValidationSummary component to summarize validation messages.

    <DxTreeList ...>
        <Columns>
            @* ... *@
        </Columns>
        <EditFormTemplate Context="editFormContext">
            <DxFormLayout>
                <DxFormLayoutItem Caption="First Name:" ColSpanMd="6">
                    @EditFormContext.GetEditor("FirstName")
                </DxFormLayoutItem>
                <DxFormLayoutItem Caption="Last Name:" ColSpanMd="6">
                    @EditFormContext.GetEditor("LastName")
                </DxFormLayoutItem>
                <DxFormLayoutItem Caption="Title:" ColSpanMd="6">
                    @EditFormContext.GetEditor("Title")
                </DxFormLayoutItem>
                <DxFormLayoutItem Caption="Hire Date:" ColSpanMd="6">
                    @EditFormContext.GetEditor("HireDate")
                </DxFormLayoutItem>
                <DxFormLayoutItem ColSpanMd="12">
                    <ValidationSummary />
                </DxFormLayoutItem>
            </DxFormLayout>
        </EditFormTemplate>
    </DxTreeList>
    

Blazor TreeList Input Validation

Custom Validation

You can create custom validator components as described in the following Microsoft topic: Validator components.

To enable custom validation in the TreeList, declare validator components in one of the following templates:

CustomValidators
Declared validators override the standard DataAnnotationsValidator. To use DataAnnotationsValidator in addition to custom validators, declare it in the CustomValidators template explicitly.
EditFormTemplate or CellEditTemplate
The TreeList uses the standard DataAnnotationsValidator and declared custom validators. Do not place the DataAnnotationsValidator in the edit form template to avoid duplicate validation messages.

The following example uses a custom validator to evaluate the Due Date field value.

@inject EmployeeTaskService EmployeeTaskService

<DxTreeList Data="TreeListData"
            KeyFieldName="Id"
            ParentKeyFieldName="ParentId"
            EditMode="TreeListEditMode.EditForm"
            CustomizeEditModel="TreeList_CustomizeEditModel"
            EditModelSaving="TreeList_EditModelSaving"
            DataItemDeleting="TreeList_DataItemDeleting">
    <Columns>
        <DxTreeListCommandColumn />
        <DxTreeListDataColumn FieldName="Name" Caption="Task" />
        <DxTreeListDataColumn FieldName="EmployeeName" />
        <DxTreeListDataColumn FieldName="StartDate" />
        <DxTreeListDataColumn FieldName="DueDate" />
    </Columns>
    <EditFormTemplate Context="EditFormContext">
        <DxFormLayout CssClass="w-100">
            <DxFormLayoutItem Caption="Task:" ColSpanMd="6">
                @EditFormContext.GetEditor("Name")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Employee Name:" ColSpanMd="6">
                @EditFormContext.GetEditor("EmployeeName")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Start Date:" ColSpanMd="6">
                @EditFormContext.GetEditor("StartDate")
            </DxFormLayoutItem>
            <DxFormLayoutItem Caption="Due Date:" ColSpanMd="6">
                @EditFormContext.GetEditor("DueDate")
            </DxFormLayoutItem>
        </DxFormLayout>
    </EditFormTemplate>
    <CustomValidators>
        <MyCustomValidator DataItemValidating="ValidateTreeListData" />
    </CustomValidators>
</DxTreeList>

@code {
    List<EmployeeTask> TreeListData { get; set; }

    protected override void OnInitialized() {
        TreeListData = EmployeeTaskService.GenerateData();
    }
    void ValidateTreeListData(ValidationMessageStoreEventArgs e) {
        var task = (EmployeeTask)e.EditModel;
        if (task.DueDate < task.StartDate) {
            e.AddError(nameof(task.DueDate), "The due date should be greater than the start date.");
        }
    }
    void TreeList_CustomizeEditModel(TreeListCustomizeEditModelEventArgs e) {
        if (e.IsNew) {
            var newTask = (EmployeeTask)e.EditModel;
            newTask.Id = TreeListData.Max(x => x.Id) + 1;
            if (e.ParentDataItem != null)
                newTask.ParentId = ((EmployeeTask)e.ParentDataItem).Id;
        }
    }
    async Task TreeList_EditModelSaving(TreeListEditModelSavingEventArgs e) {
        if (e.IsNew)
            TreeListData.Add((EmployeeTask)e.EditModel);
        else
            e.CopyChangesToDataItem();
    }
    async Task TreeList_DataItemDeleting(TreeListDataItemDeletingEventArgs e) {
        TreeListData.Remove((EmployeeTask)e.DataItem);
    }
}

Blazor TreeList Editing Custom Validation

Disable Validation

Set the ValidationEnabled option to false to disable input validation in all DevExpress data editors located in the edit form or edit cells.

<DxTreeList Data="Data"
        ...
        ValidationEnabled="false">
    @* ... *@
</DxTreeList>

To disable validation in an editor, set its ValidationEnabled property to false.