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?
// 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
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
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.