Validate a row before its deletion

Validate a row before its deletion

rrojas2018rrojas2018 Posts: 8Questions: 3Answers: 0
edited April 2017 in Free community support

Hi, please you help I need to add a validate before delete row, I have the following code, but nothing happend, the row still deleted,

if ($action == Editor::ACTION_DELETE) {
foreach ( $data['data'] as $pkey => $values ) {
if ( $values['estado'] === 'Invoiced') {
return 'No se puede eliminar';
}
}
}

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    edited April 2017

    A preRemove event handler could be used to add a suitable validator, or even just cancel the delete itself.

    Allan

  • rw152rw152 Posts: 56Questions: 15Answers: 1

    Will it be possible then to add a message to the return false inside the pre-events?

    The documentation says that errors in need of messages should be handled by the validator, but it appears the preRemove events happen first.

    In my case, I need to make sure that fields are complete before the preRemove event is fired. If the field is properly formatted, I then return false inside my preRemove event and soft delete the object.

  • rw152rw152 Posts: 56Questions: 15Answers: 1

    (for the record, I can't use a die because I'm using the PDO database connection object)

  • rw152rw152 Posts: 56Questions: 15Answers: 1

    If anyone else is struggling with this, this is what I did to get around it (per the documention on global editor validation)

        ->validator( function ( $editor, $action, $data ) {
            if ( $action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT ) {
                foreach ( $data['data'] as $pkey => $values ) {
                    if ( $values['country'] === 'US' && $values['location'] === 'London UK' ) {
                        return 'London UK is not in the US';
                    }
                }
            }
        } )
    

    https://editor.datatables.net/manual/php/validation
    Towards the bottom

This discussion has been closed.