How to return error message after canceling a preRemove server side event

How to return error message after canceling a preRemove server side event

GargiucnGargiucn Posts: 109Questions: 30Answers: 0

Following the discussion:
https://datatables.net/forums/discussion/comment/114990/#Comment_114990

I can check the data and cancel the deletion of the row using "return false"

->on( 'preRemove', function ( $editor, $id, $values ) {
  $editor->validator( function ( $editor, $action, $data ) {
    ... some validator logic

    return false;

  } );
} )

I would like to have an example of how I can also return an error message...
thank you,

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I think you've hit a limitation with the global validator here. Individual fields can return an error message, but not the global one (yet).

    Allan

  • GargiucnGargiucn Posts: 109Questions: 30Answers: 0

    I found a way faster even if not very elegant to check if the value of "DT_RowId" is present in other tables and therefore can not be deleted.

    ->on( 'preRemove', function ( $editor, $id, $values ) { 
        $idprod = substr($values['DT_RowId'],4);
        $dupli = $db
        ->raw()
        ->exec('SELECT COUNT(*) as count FROM rlistini WHERE rli_prod = .$idprod.'')
        ->fetchAll();
        foreach($dupli as $item ){
            $count = $item['count'];
        }
        if($count != 0){
            return false;
        }
    

    But the problem of how to display a warning message remains unsolved...

  • SchautDollarSchautDollar Posts: 21Questions: 6Answers: 1

    Hopefully I am not necro'ing a thread here, but I thought this would be relevant.

    When an exception is thrown, DataTables displays that message to the user.

    Is there any negative consequences in manually throwing an exception here?

    Example:

    ->on( 'preRemove', function ( $editor, $id, $values ) {
        ...
        if($count != 0){
            throw new Exception("Unable to delete, items still remain. etc etc");
        }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Not as far as I'm aware. Editor's libraries do a try/catch by default and will show the exception text to the user. It will also rollback the database since it performs its db actions in a transaction by default.

    Good suggestion that - thanks.

    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    I've just been looking at this, and I'd forgotten at the time that you can just use return 'Error message...'; in a global validator to have it show as an error! The throw suggestion above is another option.

    Allan

This discussion has been closed.