How to test that a field value is unique on the client side before submitting to the server side?

How to test that a field value is unique on the client side before submitting to the server side?

Chris-B11Chris-B11 Posts: 2Questions: 0Answers: 0
edited September 15 in Editor
// Using initSubmit to precondition data before submitting it to server

  editor.on( 'initSubmit', function (e, action) {
      if ( editor.field( 'Group' ).val() === '' ) {
         editor.field( 'Group' ).error( 'This field is required' );
         return false;
// the following code does not detect duplicates
      ****if (table.columns('.Group').search(editor.field( 'Group' ).val()) === true) {****
         editor.field( 'Group' ).error( 'This value already exists.' );
         return false;
      }
   } );

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Replies

  • colincolin Posts: 12,479Questions: 0Answers: 2,117

    This example here may help - it's colouring rows if they are duplicated in the table. You could use that approach to determine if the value exists in another record. This will only work if you're not using serverSide, as in that case only the displayed data is available, and not all the table's data.

    Colin

  • Chris-B11Chris-B11 Posts: 2Questions: 0Answers: 0

    Thank you for your response. I determined that the correct DataTables function to use was .filter(), not .search(). My working version uses the following approach.

      var filterdata = table
         .column(0)
         .data()
         .filter(function (value,index) { return value === editor.field( 'Group' ).val() ? true:false;
         } )
      if ((action === 'create') && (filterdata.count() != 0) ){
         editor.field( 'Group' ).error( 'This value already exists.' );
         return false;
      }
    
Sign In or Register to comment.