Prevent delete, if records exist in related table.

Prevent delete, if records exist in related table.

MickBMickB Posts: 103Questions: 25Answers: 2

I have an Application table and a Roles table.
I don't want to allow Applications to be deleted, if it has any Roles assigned.
To confused things further, I'm not actually doing a delete, I just edit the deleted_at field:
https://datatables.net/forums/discussion/31310/possible-to-soft-delete

I guess it may be easier, to prevent the delete button being enabled, if this Application has some Roles.
The alternative is to check the db when I set the deleted_at field, then somehow return an error.

I can't help feeling I am making this more difficult than it needs to be!

Mick

This question has an accepted answers - jump to answer

Answers

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Maybe pull back the count of child rows as a hidden column, then use the preEdit event to check this, then display a message and cancel the edit.

    How can you cancel the edit?

  • MickBMickB Posts: 103Questions: 25Answers: 2

    preRemove, then return false!

    I will try like this, unless anyone can suggest a better way.

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Done this differently now. I am using the REST interface and handle the destroy myself.

    Before the delete, I check for dependencies and if they exist, I send an error back to Datatables.

    This feels much better.

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin
    Answer ✓

    Hi,

    I presume you are using a custom button for the "delete" action since it is actually just an edit? If so, then I would suggest adding some logic into the button to check if the row can be deleted or not before then triggering the edit.

    If you aren't using a custom button, then that is probably the way to go (docs).

    Allan

  • MickBMickB Posts: 103Questions: 25Answers: 2
    edited January 2016

    I was catching the action in the presubmit and swapping for an edit.

    I have got this working OK using the REST interface which feels much better when using Laravel, that way I can use my model layer.

    Mick

This discussion has been closed.