Validation on MultiEdit

Validation on MultiEdit

marwimarwi Posts: 33Questions: 9Answers: 0

Hello,
I have setup a self-join in a checkbox. In this checkbox I can select table names, while the DB stores the corresponding table id . As this checkbox is an optional input field, I can also select the value 'None' and the DB should store 0 (zero). This all works fine when editing single records.
Problem: If I select two (or more) records for MultiEdit, while one record's checkbox is set to 'None' and the other's checkbox is NOT set to 'None', the validator does not let me save any change and throws the message "Input not valid".

This is my setup:

/* JAVASCRIPT File: */

var dt_tables_editor = new $.fn.dataTable.Editor( {
    ...
    fields: [
        { "label": "db_table:", "name": "dt_tables.db_table", "type": "textarea"  },
        { "label": "parent_table_id:", "name": "dt_tables.parent_table_id", "type": "select" , placeholder:"none", placeholderDisabled:false }
    ]
});

var dt_tables_table = $('#dt_tables').DataTable( {
    ...
    columns: [
        { data: 'dt_tables.db_table' },
        { data: 'dt_tables2.db_table' }
    ]
});
            

/* PHP File: */

Editor::inst( $db, 'dt_tables', 'id' )
    ->fields(
        Field::inst( 'dt_tables.db_table' ),
        Field::inst( 'dt_tables.parent_table_id' )->options(Options::inst()->table('dt_tables')->value('id')->label('db_table'))->validator(Validate::dbValues()),
        Field::inst( 'dt_tables2.db_table' )
    )->leftJoin('dt_tables as dt_tables2','dt_tables2.id','=','dt_tables.parent_table_id')
    ->process( $_POST )
    ->json();

Is it possible to fix this issue so I can do MultiEdit somehow?

Best regards
marwi

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    Can you show me the data that is being submitted from the client-side please ("Network" inspector and then the "Headers" for the Ajax request).

    The row validation should be done on a per row basis, even when multi-editing. So if both pass when single row editing, they should also pass when multi-row editing.

    Allan

  • marwimarwi Posts: 33Questions: 9Answers: 0

    Hi Allan,
    Sure this is the data. Please let me know if you need anything else to get the scenario.

    Request Header:
    ---------------
    Host: (mydomain.com)
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:58.0) Gecko/20100101 Firefox/58.0
    Accept: application/json, text/javascript, */*; q=0.01
    Accept-Language: de,en-US;q=0.7,en;q=0.3
    Accept-Encoding: gzip, deflate
    Referer: http://(mydomain.com)/dt_tables.php
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    X-Requested-With: XMLHttpRequest
    Content-Length: 1445
    Connection: keep-alive
    
    
    Response Header:
    ----------------
    Server: nginx
    Date: Tue, 27 Feb 2018 14:04:04 GMT
    Content-Type: text/html; charset=UTF-8
    Content-Length: 102
    Connection: keep-alive
    Vary: Accept-Encoding
    Content-Encoding: gzip
    X-Powered-By: (myhoster.com), PleskLin
    
    Data (Extract):
    ---------------
    action  edit
    data[row_4][dt_tables][db_table]    sessions
    data[row_4][dt_tables][parent_table_id] 0
    data[row_8][dt_tables][db_table]    turnarounds
    data[row_8][dt_tables][parent_table_id] 5
    
    Response:
    ---------
    
    {"fieldErrors":[{"name":"dt_tables.parent_table_id","status":"Input not valid"}],"data":[]}
    
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    I've got a feeling its the 0 value.

    Do you have another row where the parent_table_id is not 0 and you can try updating that row and the 5 row at the same time?

    Thanks,
    Allan

  • marwimarwi Posts: 33Questions: 9Answers: 0

    Yes, this only happens if I mix 0 value and Non-0 value. If I update two rows with both having 0 value or two rows with both having Non-0 values, it works fine. But the mix throws the validation error. The 0-value is not an existing id in the table, but I use this value 0 for indicating that the user did select 'None' in the dropdown list, as this selection is optional.

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    edited March 2018 Answer ✓

    I see - so the 0 isn't actually in the database table? Its odd that it passes at all when you submit a 0 for that field then!

    The dbValues validator has the option of specifying extra values that are valid beyond just those used in the database. In this case you might use:

    Validate::dbValues( null, null, null, null, [ 0 ] )
    

    (that 0 might need to be in quotes to make it a string....)

    Allan

  • marwimarwi Posts: 33Questions: 9Answers: 0

    Yes, it works now with those parameters you have provided. There is one comma missing before the [ 0 ].

    However, also see that it's strange as the validator did not throw an error in case of single edit with 0 before. I also did not define any placeholderValue ... I'm not fully understanding the logic behind, but at least it work fine now, thanks for your great help!

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    Oops - thanks for spotting the error. I've edited it now.

    Yes, I don't understand why it didn't throw an error for the single edit of a 0 value before either. I'll try to replicated that locally. It sounds like a falsy test error.

    Allan

This discussion has been closed.