I am writing a validation callback method for checking the duplicates in devexpress grid component
ts file
validationCallback(e: any) {
let isValid = true;
for (let i = 0; i < this.abbreviation_array.length; i++) {
if (this.abbreviation_array[i] === e.value && e.value !== undefined ) {
isValid = false;
break;
}
}
if (isValid === true ) {
this.abbreviation_array.push(e.value);
this.abbrev_flag = false;
}
else {
this.abbrev_flag = true;
}
return isValid;
}
html file
<dxi-column dataField="Abbreviation">
<dxi-validation-rule type="custom" [reevaluate]= "false" [validationCallback]="validationCallback">
</dxi-validation-rule>
</dxi-column>```
But while coming to first element or any other previous element it puts flag to true as it is entered in array and validation_callback is evaluated everytime the cell is entered with value.
Please suggest how to solve this
2
Answers
populate the abbreviation array on initializing the grid like so
Then change the validation to just check the array!
To prevent the flag from being set to true when entering a value that already exists in the array, you can modify the validation callback method to exclude the current row’s value from the comparison. Here’s how you can do it:
In this modified version, e.rowIndex is used to identify the current row being edited. When checking for duplicates, it skips the comparison for the current row’s value by excluding its index from the comparison. This way, the flag won’t be set to true when editing the same value in the same row.