简体   繁体   中英

How to add required validation in dx-list of devextreme control for angular?

I want to add required field validation in dx-list of devextreme control for angular.

Here is my html snippet.

<dx-list class="checkbox-list" id="chkCentreCareTypes" [dataSource]="careTypes" (onSelectionChanged)="onCareTypeChanged($event)"
                                    selectionMode="multiple" displayExpr="text" valueExpr="value" [showSelectionControls]="true">
                                </dx-list>

And Output of this is as follows.

在此处输入图像描述

Everything starts with the following concept; DxList is not considered a ui edit, so dxValidate will not work naturally for it.

So you need to devinis a custom validation for it.

Below is a simple code on how to do this.

html

<dx-list #listx>
   <dx-validator [adapter]="adapterConfig" validationGroup="validationGroup">
      <dxi-validation-rule type="custom" [validationCallback]="validationCallback"></dxi-validation-rule>
      </dx-validator>
   </dx-list>

ts

@ViewChild("listx", {static: false}) listx: DxListComponent;

borderStyle: string = "none";
adapterConfig = {
   getValue: () => {
      return this.listx?.selectedItems?.length; //-- value to be validated
   },
   applyValidationResults: (e) => {
      this.borderStyle = e.isValid ? "none" : "1px solid red";
   }
};

validationCallback(params) {
   return params.value!=0;
}

Include reset function

adapterConfig = {
   getValue: () => {
      return this.listx?.selectedItems?.length; //-- value to be validated
   },
   applyValidationResults: (e) => {
      this.borderStyle = e.isValid ? "none" : "1px solid red";
   },
   reset: () => {
      this.fluxoItems;
   }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM