PreRemove check (server side) with friendly warning to user

PreRemove check (server side) with friendly warning to user

bsukbsuk Posts: 92Questions: 26Answers: 2
edited March 16 in Editor

Hi, as per:

I'm very interested in Allan's suggestion at the bottom of this thread.
However, when I attempt this code (with my validation being simply: "return false;" - whatever I specify, the row is still deleted without warning.

What I want is to be able to do a check (using custom function logic), then if the check fails, return a validation error to the user on the delete modal somehow - saying "Deletion isn't possible (e.g because associated records still exist)"

Is this possible?

My controller code:

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'examiners')
        Field::inst( 'id' )->set( false ),
        Field::inst( 'title' ),
        Field::inst( 'firstname' )
            ->validator( Validate::notEmpty( ValidateOptions::inst()
                ->message( 'A first name is required' ) 
            ) ),
        Field::inst( 'lastname' )
            ->validator( Validate::notEmpty( ValidateOptions::inst()
                ->message( 'A last name is required' )  
            ) ),
        Field::inst( 'notes' )
    ->on( 'preRemove', function ( $editor, $id, $values ) {
        //return false;
        $editor->validator( function ( $editor, $action, $data ) {
                    // My custom check would go here?  On fail....
                    return false;
                    // Or would it be somehow...
                    echo "My friendly user error goes here, then cancel delete"; return false;
          } );
    } )
    ->process( $_POST )

Notice the hashed out "return false;" line. With that unhashed, the row does not delete (which is great), however, there is no mechanism to inform the user that the row has intentionally failed to be deleted.


  • allanallan Posts: 54,528Questions: 1Answers: 8,526 Site admin

    You would need to listen for the postSubmit event handler and check the error property in the returned JSON. If there is an error message, you could throw it up in a friendly message box.

    Eventually we'll provide an API in Editor for such a message box, but it isn't something that we've added in yet.


  • bsukbsuk Posts: 92Questions: 26Answers: 2

    Thanks, Allan.
    I'll look into that.

    Though thinking about it more, it might be preferential to run the logic at an earlier stage, so as not to allow the user to try and delete the row in the first place.
    I guess another option is an Ajax call from a client side event that can determine a different outcome (warning/disabled delete button etc.)
    I'll post my findings back here if I come up with a useful workaround.

    The situation is that I have a joined table with records that have foreign keys linked to others with cascading. I want to ensure that the user can only delete records that aren't linked to live data that's in use (without realising that they would be deleting other records also, due to the cascade setting).

  • allanallan Posts: 54,528Questions: 1Answers: 8,526 Site admin

    I'd say a validator is the correct thing to use here - although I probably won't add it with an event handler, just have it always there and check to see what action is being submitted ($editor->action()). If it isn't a delete action, then just pass the validator (return true).

    But yes, you might need to query the db to check that the record can indeed be safely deleted.


Sign In or Register to comment.