I'm working on an editable grid that the user can add rows to. I'm trying to validate that a newly added row doesn't already exist with the same values somewhere else in the grid. I have a row validator that checks all the rows and will put the new row into an error state if a duplicate is found.
The problem that I can't seem to find a solution for is if the user goes to the already existing row and makes changes to fix the error, the new row that was invalid cannot be revalidated to clear the error.I tried to prevent leaving the new row by canceling the `rowEdit` event, but canceling removes the row entirely. It's odd.
Is there a way to have the grid run the validation for all rows and all cells?
Thank you.
Hello Joshua,
Thank you for posting to Infragistics Community!
I have been looking into your question and I am wondering if you are leveraging the Grid Editing and Validation functionality to implement validation in this case? This is a relatively new feature introduced in our library since version 14.1.0. With it, the validation mechanism is triggered when editing both cells and rows. This leaves me under the impression that the validation on your side might be implemented by other, possibly custom approaches. Please, let me know if my assumptions are incorrect and do elaborate further in that case.
So, what I can suggest is checking out the built-in grid validation features. Additionally, unique validator is not among the supported out-of-the-box, however custom validators could also be set, as described in this section. As an example, please check out this StackBlitz demo, where a custom “unique values” validator is implemented and applied to the “ProductName” field.
In conclusion, please, check out the referenced resources and let me know if you need any further assistance on the matter.
Best regards, Bozhidara Pachilova Associate Software Developer
Thank you!I am attempting to use the new Grid Editing and Validation functionality and it has been working well. But the version of the igx-grid I'm working with is a somewhat custom implementation and the developer that implemented it has moved on to a new company and I'm left to figure it out on my own.I really like the example you provided. I will try and implement it and see how it goes. Thank you again.
Hi Joshua,
Thank you for following up! Sure, take your time to test the suggested approach and let me know if I can assist you with anything else on the matter.
Best regards, Bozhidara Pachilova
I implemented the Directive and it works, but it doesn't solve the problem I mentioned in my original post. The problem is reproducible in the provided demo project.If you change ProductName in the first row to match the value in the second row, you get an error. And that is expected. But if you change ProductName in the second row to something else, the first row is still in an error state even though the rule is no longer broken.
I need a way to revalidate the invalid row. A solution to this issue might help me with a different issue where I would like the validate all the rows when loaded into the grid for the first time.Thanks again,Josh
Thank you for following up.
As documented in the Validation triggers section of the Grid Validation topic, validation is not triggered for cells/rows that are not edited via user input or via the editing API.
Having this in mind, the sequence that you are describing can still be achieved by implementing additional logic in this direction. I modified the previous sample to demonstrate a possible approach when row editing is allowed and by leveraging the grid’s updateRow method.
The initial invalid state could be “triggered” in the ngAfterViewInit hook:
ngAfterViewInit(): void { this.data.forEach((entry) => { requestAnimationFrame(() => { this.grid.updateRow(entry, entry[this.grid.primaryKey]); }); }); }
And for the described row editing scenario in order to retrigger validation for the other invalid records that no longer should have errors after the particular row edit is finished, the rowEditDone event could be leveraged, for example:
public rowEditDone(event) { let recValidationState: IRecordValidationState[] = this.grid.validation.getInvalid(); recValidationState.forEach((rv) => { rv.fields.forEach((f) => { if (f.errors && f.errors.duplicateValue) { let rowData = this.grid.getRowData(rv.key); requestAnimationFrame(() => { this.grid.updateRow(rowData, rowData[this.grid.primaryKey]); }); } }); }); }
Please, feel free to further modify this solution to fit your requirements. I hope it helps.
This was what I needed. I had to implement your solution slightly different since we're not using rowEditMode and I have some service calls that I have to wait for, but it totally works how I need it to.Thank you!
Hi,
I am glad that you find my suggestions helpful. Thank you for using Infragistics components!