onPreSubmit doesn't seem to work for me...

onPreSubmit doesn't seem to work for me...

mikemikemikemike Posts: 4Questions: 0Answers: 0
edited September 2013 in Editor
I am trying to have some sort of control on the table data before sending to the server and so I decided to use onPreSubmit.
To check if I was doing right, I tried the following:
[code]
var editor;
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": tUrl, // php
"domTable": "…",
"fields": [ …]
});//end editor
...
...
editor.on ( 'onPreSubmit',
function (e, data) {
var d = data.nome;
alert("hey, you are " + d +"!");
return false; // just to be safe
}
);

});//end $.(document).ready
[/code]
When clicking the submit button on editor's form, I expected to see an alert with the message "hey, you are -name on the form- !",
but the alert message instead says: "hey, you are undefined!"
'nome' is a field which is correctly loaded and displayed both in the table and in editor's form.

…I can't understand why the 'data.nome' property turns out to be undefined.

What am I doing wrong?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Its actually `data.data.nome` that you want here. The second parameter passed into `onPreSubmit` is the full data object, including the call type, id etc and the information for the form is held in the `data` parameter. See: http://editor.datatables.net/server .

    Allam
  • mikemikemikemike Posts: 4Questions: 0Answers: 0
    Thank you very much Allan for the prompt response and solution to my mistake!
    I was going along with the example shown on the API page regarding events fired by Editor:

    [code]
    // Client-side validation using onPreSubmit
    editor.on( 'onPreSubmit', function (e, data) {
    if ( data.name === "" ) {
    this.error('name', 'A name must be given');
    return false;
    }
    else if ( data.name.length >= 10 ) {
    this.error('name', 'The name length must '+
    'be less that 10 characters');
    return false;
    }
    // ... additional fields could be checked here
    } );
    [/code]

    ..obviously, I was missing something about the object's structure..

    Michele
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    edited September 2013
    No - entirely my mistake - my apologies! The example code you pasted above is in error.

    This example shows how it should work:
    http://editor.datatables.net/release/DataTables/extras/Editor/examples/clientValidation.html

    I'll fix that page forthwith (edit - now done).

    Thanks,
    Allan
  • mikemikemikemike Posts: 4Questions: 0Answers: 0
    Glad I helped in fixing a typo :)
    Huge documentation. Beautiful job.

    regards,
    Michele
    (and now.. back to my regex headaches..(: )
This discussion has been closed.