# Angular TreeList Editing Validation

The [Angular TreeList](https://www.telerik.com/kendo-angular-ui/components/treelist) supports validation for editing operations, allowing you to ensure data integrity and provide user feedback when validation fails. You can implement validation both when using [built-in editors](https://www.telerik.com/kendo-angular-ui/components/treelist/editing#built-in-cell-editor-types) and [custom editor templates](https://www.telerik.com/kendo-angular-ui/components/treelist/editing/custom-editors) with [Reactive Forms](https://angular.dev/guide/forms/reactive-forms).

The following example demonstrates how to enable validation with built-in editors, including required field validation.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/editing/editing-directives/reactive-editing/source.txt)

## Validation with Built-in Editors

When you use the TreeList's [built-in editing directives](https://www.telerik.com/kendo-angular-ui/components/treelist/editing#quick-setup), validation is handled through the `FormGroup` created by the [`createFormGroup`](https://www.telerik.com/kendo-angular-ui/components/treelist/api/createformgroupargs) callback function. The TreeList automatically prevents saving changes when the form is invalid and provides visual feedback through the built-in validation styling.

```typescript
public createFormGroup = (args: CreateFormGroupArgs): FormGroup => {
    const item = args.isNew ? {} : args.dataItem;

    return this.formBuilder.group({
        id: item.id,
        name: [item.name, Validators.required],
        role: [item.role, Validators.required],
        yearsExperience: [
            item.yearsExperience,
            Validators.compose([Validators.required, Validators.min(0)])
        ],
    });
};
```

## Validation with Custom Editor Templates

When you use custom editor templates, you can display validation messages by accessing the `formGroup` field passed by the [`EditTemplateDirective`](https://www.telerik.com/kendo-angular-ui/components/treelist/api/edittemplatedirective). Use the form state properties such as `invalid`, `untouched`, and others to toggle validation messages dynamically.

The following example demonstrates how to render validation messages inside a [Kendo UI for Angular Popup](https://www.telerik.com/kendo-angular-ui/components/popup) that appears when values entered in the custom cell editors are invalid.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/editing/custom-reactive-editing/source.txt)

To implement Popup validation messages in your TreeList editing templates, follow these steps:

1. Create a custom directive to expose the `ElementRef` of the custom editor component. This directive serves as an anchor point for the Popup positioning.

   ```typescript
   @Directive({
       selector: '[popupAnchor]',
       exportAs: 'popupAnchor'
   })
   export class PopupAnchorDirective {
       constructor(public element: ElementRef) {}
   }
   ```

2. Apply the directive to your custom editor component and create a template reference that serves as the [`anchor`](https://www.telerik.com/kendo-angular-ui/components/popup/api/popupcomponent#anchor) of the Popup.

   ```html
   <ng-template kendoTreeListEditTemplate let-column="column" let-formGroup="formGroup" let-isNew="isNew">
       <kendo-autocomplete
           #anchor="popupAnchor"
           popupAnchor
           placeholder="Select position..."
           [data]="suggestions"
           [formControl]="formGroup.get(column.field)">
       </kendo-autocomplete>
       <kendo-popup [anchor]="anchor.element" ...>
           Position is required
       </kendo-popup>
   </ng-template>
   ```

3. Use Angular's `@if` control flow block to conditionally display the Popup based on the validation state of the associated `FormControl` for the current column field. Include an SVG icon to enhance the validation message presentation.

   ```html
   <ng-template kendoTreeListEditTemplate let-column="column" let-formGroup="formGroup" let-isNew="isNew">
       <kendo-numerictextbox
           #anchor="popupAnchor"
           popupAnchor
           format="#"
           [formControl]="formGroup.get(column.field)">
       </kendo-numerictextbox>
       @if (formGroup.get(column.field).invalid && !(isNew && formGroup.get(column.field).untouched)) {
       <kendo-popup [anchor]="anchor.element" popupClass="k-widget k-tooltip k-tooltip-validation k-invalid-msg">
           <kendo-svgicon [icon]="exclamationIcon" size="xlarge"></kendo-svgicon>
           Extension must be a positive number
       </kendo-popup>
       }
   </ng-template>
   ```

## See Also

* [Angular TreeList Editing Basics](https://www.telerik.com/kendo-angular-ui/components/treelist/editing)
* [Custom TreeList Editors](https://www.telerik.com/kendo-angular-ui/components/treelist/editing/custom-editors)
* [Angular Reactive Forms Validation](https://angular.dev/guide/forms/form-validation)
* [API Reference of the TreeListComponent](https://www.telerik.com/kendo-angular-ui/components/treelist/api/treelistcomponent)
* [Popup Overview](https://www.telerik.com/kendo-angular-ui/components/popup)
