Hi,
I have situation when I need to manage validationRules inside Grid (cell edit) not for Column definition but in case of every row differently.
When user will select accountCode (first edited column) - based on this information I need to set required=true
for validation for specific columns list and I want to show information for user that selected field are required (as standard validationRules for columns works). List of columns for every row could be different.
I have tried to do this as below:
I have grid definition with some columns and batch edit mode (without declaration of 'validationRules').
Then if user will select element in first column - based on this value I have update 'validationRules' inside cellEdit for the other of columns list. Values has been save correctly but as I understand it is not working becase Grid is no re-rendered / row refreshing - that this updated rules do not work.
Do we have some option to refresh row without loosing actual editing data and to run this new validationRules?
Or do we have some other option to manual validate field value or run validation inside edit?
Does anyone have any suggestions on the above topic? I cannot find any solution for that.
Hi Karol Statek,
Sorry for the inconvenience caused.
We have reviewed your query about adding validation rules for each row based on the edited value of another column.
Image:
Can you please provide the above information which will be very helpful for us to provide better solution.
Regards,
Dineshnarasimman
testsdf
Hi Dineshnarasimman,
Ad 1. After selecting my 'main' column value, I received from api details of which element/grid column should be required and which not. It would be perfect if user will not be able to save edited row/cancel cell edit - with the same way as default grid and required column works.
Ad 2. Based on your example at the begining I don't have any validation rules for column Customer Name (not required).
Then if I will select ShipCountry to Brazil - I would like to set required row element in Customer Name column (only for actual editing row). So if user will select Brazil, he cannot save row/grid without entering Customer Name value for this row.
In other ases if user will select ShipCountry to France - Customer Name will not be required - could be empty.
In my case my "main" (ShipCountry) column is at begining of columns list - but as I supposed column order doesn't matter to show main issue.
I have tried to refresh grid columns but updated validationRules doesn't work.
I would be gratefull for some suggestions.
Hi Karol Statek,
Based on your query, we understand that you want to prevent the submission of empty values for a particular column based on the value of another column. This can be achieved by using the Custom Validation feature of the Grid. We have developed the validation logic that will help you achieve the desired outcome. Please find the code snippet and sample below for your reference:
|
[index.js]
// custom validation function function customerNameValidationFunc(args) { let rowIndex = args.element.closest('.e-row').rowIndex; let rowObject = grid.getRowsObject()[rowIndex]; let rowBatchData = rowObject.changes ?? rowObject.data;
// includes the countries which requires name let customerNameRequiredCountries = ['Brazil', 'Germany'];
// check row data with particular country has empty name let required = !( args.element.value === '' && customerNameRequiredCountries.includes(rowBatchData.ShipCountry) );
return required; }
// validation rule const customerNameValidationRule = { required: [customerNameValidationFunc, 'Customer Name required !'], };
. . . . .
<ColumnDirective field="CustomerName headerText="Customer Name" width="150" validationRules={customerNameValidationRule} ></ColumnDirective>
|
Sample: https://stackblitz.com/edit/react-grid-custom-validation
In the sample, we have implemented logic to make the “CustomerName” column required for the “ShipCountry” values “Brazil” and “Germany” based on your explanation. For other “ShipCountry” values, the “CustomerName” column could be empty.
You can find more information about Custom Validation in the documentation:
Documentation: https://ej2.syncfusion.com/react/documentation/grid/editing/validation#custom-validation
Note: We have prepared this logic based on the explanation provided in your last response. You can modify the logic based on your requirements if necessary.
Please feel free to reach out if you have any further queries or require additional assistance.
Regards,
Santhosh I