DataTables Editor - client-side validation example

In most of my examples I've assumed that server-side validation of the data will be performed and the returns reported back to the client (the validation must be done at the server anyway, so this saves duplicating logic), however there are cases where performing client-side checks can be very valuable. There are a number of methods of doing this with Editor including simply not calling the submit method when you don't want to submit the data, but this example shows how client-side validation can be achieved using the onPreSubmit event.

Editor provides a wide range of events which can be bound at initialisation time, or after initialisation using the on API method. In this case the onPreSubmit event is used to listen for the event that occurs just before the data is submitted to the server, and we can return false to stop that request being made based on the data that would be submitted to the server. The error method is used to show the error message to the end user (note that the callback functions are executed in the Editor instance's scope, so API methods can accessed using the this keyword.

Browser Rendering engine Platform(s) Engine version CSS grade
Browser Rendering engine Platform(s) Engine version CSS grade

Initialisation code

The Javascript below is what is used on this page for the example shown.


				

Table HTML

The HTML shown below shows the TABLE that is used in this example.


				

Server-side PHP

The code below is that HTML that is used to drive the server-side section of this exampe.


				

Included media files:

The HTML shown below includes the Javascript and CSS files that are required for the Editor operation on this page. You can use this as a template for your own pages, but if you move the files you might need to update the paths.


				

Communication of Editor between the client and server:

Editor submits and retrieves information by Ajax requests. The two blocks below show the data that Editor submits and receives, to and from the server. This is updated live as you interact with Editor so you can see what is submitted.

Submitted data:

The following shows the data that has been submitted to the server when a request is made to add, edit or delete data from the table.

// No data yet submitted

Server response:

The following shows the data that has been returned by the server in response to the data submitted on the left and is then acted upon.

// No data yet received

Data for DataTables from server:

DataTables can request information to display from the server when the page first loads. The block below shows the response from the server.


				

Examples

Basic:
Advanced: