setFormatter( Format::ifEmpty( null ) ) at the same time as a setValue

setFormatter( Format::ifEmpty( null ) ) at the same time as a setValue

greggreggreggreggreggreg Posts: 42Questions: 19Answers: 2
edited March 2019 in Free community support

I am doing a soft delete. For the most part i have followed this: https://editor.datatables.net/examples/api/softDelete

However, I want to set my time on the field server side. i.e. when I edit, I want the field to remain blank, but when I delete, I want it to set the current time. I have this on the server:

                Field::inst('deleted_datetime' )
                    ->setFormatter( Format::ifEmpty( null ) )
                    ->setValue(date("Y-m-d H:i:s"))

It works for a delete (I send the client side datetime, but ignore it with the setValue option), but for a edit when I send a empty string from the client, it also sets the field value to the current date/time.

Does anyone know how to do this?

Answers

  • allanallan Posts: 63,482Questions: 1Answers: 10,467 Site admin

    Yes, use the preRemove event to call the setValue() method only on delete.

    Allan

  • greggreggreggreggreggreg Posts: 42Questions: 19Answers: 2

    ta,

    I think thats part of my issue though. Its not a Delete, its a soft-delete, which is essentially a edit. But I do not want to set it on a normal edit. How do I distinguish between a soft-delete edit and a normal edit?

  • allanallan Posts: 63,482Questions: 1Answers: 10,467 Site admin

    I'm with you now. What you would need to do is check in the preEdit event handler if the user has submitted a soft delete action or not. If we take this example you would check to see if users.removed_date is in the data submitted or not.

    ->on('preEdit', function ($editor, $id, $values) {
      if ( $values['users']['removed_date'] ) {
        // Is a soft delete.
      }
    } );
    

    Allan

  • greggreggreggreggreggreg Posts: 42Questions: 19Answers: 2

    love your work, thank you

This discussion has been closed.