Grid custom validation across multiple fields while adding a record

Hi,

I am using grid with Dialog editing mode which allows 'Add', 'Edit' and 'Delete' operation and have a requirement to only allow a unique combination within the grid for certain fields.

For example, there is a grid with three columns - Student Id, Subject Id, Score. (There is another column Row Id which is the Primary Key but not visible on UI).

The requirement is - while adding a new record, if the combination of Student Id, Subject Id already exists in the grid, the user should not be allowed to add the record. Instead, he should see a message something like "The record already exists for this Student, Subject combination. Please update the existing record if you need to update the score". 

Can you please suggest how could this be achieved?

Thanks!


3 Replies 1 reply marked as answer

VS Vikram Sundararajan Syncfusion Team July 26, 2023 11:54 AM UTC

Hi Tera P Keplinger,


Greetings from Syncfusion support,


Based on your requirement for setting custom validation across multiple fields while adding a record. To address your specific need, we have created a sample that demonstrates how to achieve this functionality. In the sample, whenever a new record is added or edited, we check if the combination of Student ID and Subject ID already exists. If a duplicate entry is found, an alert message will be displayed, guiding you to update the existing record instead of adding a new one. Please refer the below code snippet and sample for more information


[index.js]

 

 actionBegin: function (args) {

    if (args.requestType === 'save') {

      if (args.action === 'add' || args.action=='edit') {

        var newRecord = args.data;

        var existingRecord = data.find(function (record) {

          return (

            record.StudentID === newRecord.StudentID &&

            record.SujectID === newRecord.SujectID

          );

        });

        if (existingRecord) {

          alert(

            'The record already exists for this Student, Subject combination. 

Please update the existing record if you need to update the score.'

          );

          args.cancel = true// Cancel the save operation

        }

      }

    }

  },

 


Sample- https://stackblitz.com/edit/kfnyhq-ma83mh?file=index.html,index.js


If you require further assistance, please do not hesitate to contact us. We are always here to help you.

Regards,

Vikram S


Marked as answer

JA Joseph A Whelan July 26, 2023 01:38 PM UTC

Thank you for the prompt response and the solution!



SG Suganya Gopinath Syncfusion Team July 27, 2023 04:31 AM UTC

We are glad that the provided solution helped to solve the issue. 


Loader.
Up arrow icon