->data() with custom validation and throw message...?

->data() with custom validation and throw message...?

OSFOSF Posts: 19Questions: 1Answers: 0
edited June 1 in Editor

Hello,

i use datatables editor with ->data()...

  $data = Editor::inst( $db, 'MY_TABLE', 'Id' )
  ...shorted...
  ->process( $_POST )
  ->data();

  // Additional example data...
  $addtionalData['foo'] = "bar";

  $result = [
    'data' => $data['data'],
    'additional' => $addtionalData
  ];

  echo json_encode($result, JSON_PRETTY_PRINT );

My Problem is now, send error messages via throw, back to the editor. As Example:

  ->on('preRemove', function($editor, $id, $values) {
    if ( $id == 1 ) {
      throw new Exception('<span>Example error (this item cannot removed!)</span>');
    };
  })

The throw will not execute. After click the 'remove' button on the editor modal frontend, the editor close without any messages and remove the item.
If i remove the ->data(); and change to ->json(); it works well...

Answers

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    You are copying across the data parameter to your $result assoc. array, but not any of the other fields - e.g. fieldErrors or error.

    Why not just do:

    $data['additional'] = 'foo';
    
    echo json_encode($data, JSON_PRETTY_PRINT );
    

    That way you are returning the original assoc. array that Editor builds with the data to send back to the client?

    Allan

Sign In or Register to comment.