data validation using Json inside "onPreSubmit"

data validation using Json inside "onPreSubmit"

vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
edited January 2013 in Editor
I am using latest version of datatable editor. I am looking for a solution to compare the selected data with json data (data already available in database- in order to avoid repeated entry).
[code]
"events": {
"onPreSubmit": function ( o ) {


function(json) {
for (var i = 0; i < json.length; i++) {
///item is the field name and json response itemList gives me all the available in database //
if ( o.data.item == "json[i].item" ) { this.error('item', 'item already exists in database');return false;}

};

}}
[/code]
Let me know what i am missing here. Thank you

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    I'd do this on the server-side personally. You could do it on the client-side, but you'd just need to reimplement it on the server-side anyway for security reasons.

    For doing it on the server, have a look at this thread:
    http://datatables.net/forums/discussion/12650/check-for-duplicates/p1

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    edited January 2013
    Hi Allan,
    Thanks for the quick response. That solves the duplication issue by adding the validation function in server-side.
    I am interested in client-side as I can use same json data while using join instances
    [code]
    "events": {
    "onPreSubmit": function ( o ) {

    //// is there any problem in calling the function like this?
    function(json) {
    for (var i = 0; i < json.length; i++) {
    /// I am trying for corresponding values of json data
    if ( o.data.item == "json[i].item" ) { o.data.item_code=json[i].item_catagory +'-'+some thing }

    };

    }}

    [/code]
    Do you have a solution for this please.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Yes there is a problem there, because you aren't executing the function - you are only defining it.

    > I am interested in client-side as I can use same json data while using join instances

    I must admit, I don't actually understand what you are trying to do - sorry. You want to prevent duplicates on the client-side? In which case you'd need to use fnGetData to get the data in the table, and loop over that checking for duplicates.

    But as I say, I (personally) don't really see the point in that, since you need to implement it on the server as well.

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Hi Allan,
    I solved duplication issue.
    [quote]allan said: I don't actually understand what you are trying to do[/quote]
    Here I am trying to get, another column value from same database table which is corresponding to the select-field value. The select options are drawn from database another db table as json (lable-value_ pair).
    [code]
    $out['itemList'] = $db
    ->select( 'items', 'id as value, name as label, catagory as item_catagory' )
    ///here item catagory is added as another value
    ->fetchAll();
    [/code]
    I am trying to use this json to get corresponding values according to the user selection of form data.
    and wish to apply it for a hidden field 'item_code'.
    [code]
    "events": {
    "onPreSubmit": function ( o ) {

    //// is there any problem in calling the function like this?
    function(json) {
    for (var i = 0; i < json.length; i++) {
    /// I am trying for corresponding values from json data
    if ( o.data.item == "json[i].item" ) { o.data.item_code=json[i].item_catagory .... }

    };

    }}
    [/code]
    In simple words, with a single selection of form field, we need to get two corresponding values - one is id and another is catagory.

    Hence I am requesting your help in correction of the code in second part.
    Thanking you.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Okay - so you want to transform the data being submitted based on the value from the original JSON data.

    So, I presume you are storing the JSON from the `get` request somewhere so you can access it? Let's assume that's in a locally accessible variable called `json` . At which point, you just need to modify your "function" a bit:

    [code]
    "events": {
    "onPreSubmit": function ( o ) {
    for (var i = 0; i < json.length; i++) {
    if ( o.data.item == json[i].item ) {
    o.data.item_code = json[i].item_catagory ....
    break;
    }
    };
    }
    }
    [/code]

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Hi Allan,
    I am so grateful to you.
    I have solved the issue as directed by you. Fetched the json from "fnInitComplete": function ( settings, json ) and assigned for a global variable, then used this global variable in "onPreSubmit": function ( o ).
    Once again thank you so much for your timely help.
This discussion has been closed.