initSubmit

Since: Editor 1.7

Start of the submit process, allowing changes to be made to the form.
Please note - this property requires the Editor extension for DataTables.

Description

It can be useful at times to be able to modify a form's values immediately prior to the data being submitted to the server. The preSubmit option provides access to the raw data to be submitted to the server, but it is triggered after the data has been read from the form, so any changes to the data in the form won't be reflected in the data.

This initSubmit event fills that gap, being triggered immediately before the data is read from the form, and giving the option of modifying the form.

This event is cancellable - i.e. you can return false from the callback function to stop the form from being submitted. This can be useful if you want to use local confirmation or validation.

Type

function( e, action )

Parameters:

Returns:

boolean or Promise:
Return false to stop the form being submitted.

        Alternatively (as of Editor 1.8), a Promise can be returned which should evaluate to `true` or `false`. If a promise is returned, Editor will halt its code execution until the promise is resolved. If the promise is resolved to be `false`, the form will not be submitted.

Examples

Set a value on submit, if a condition is met:

editor.on('initSubmit', function (e, action) {
	if (editor.field('section').val() === 31) {
		editor.field('rank').val('Undisclosed');
	}
});

Disallow submission if a condition is met:

editor.on('initSubmit', function (e, action) {
	if (editor.field('section').val() === 31) {
		editor.field('rank').error('Section 31 does not exist.');
		return false;
	}
});

Ajax call for user validation:

editor.on('initSubmit', function (e, action) {
	return new Promise(function (resolve) {
		$.ajax({
			url: '/api/validateUser',
			success: function (json) {
				resolve(json.allowed);
			}
		});
	});
});

Related

The following options are directly related and may also be useful in your application development.