saveErrorHandlers - how to close edit mode after handling the error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bandtank
    Active Community Member
    • Mar 2017
    • 416

    #1

    saveErrorHandlers - how to close edit mode after handling the error

    Using the information here: https://docs.espocrm.com/development...rror-handlers/

    In my system, users need to create entities called Sessions that are based on client availability. The beforeSave hook processes complex business rules to determine availablity, and then throws an error if the client is not avaiable. Using a saveErrorHandler, I give the user an option to save anyway, which mostly works.

    However, if the user decides to save the record, the page ends up in a weird state that is not quite edit mode and not quite detail mode. I don't know how to handle a successful save and the documentation does not give any information about this situation at all.

    This is what happens when the user clicks "Save Anyway" in a modal after being informed about the availability problem:

    Click image for larger version  Name:	CleanShot 2026-05-22 at 18.55.23@2x.png Views:	0 Size:	71.9 KB ID:	126523

    The record is successfully saved and most of the fields are put into read-only mode, but the "Cancel" button is still visible and the URL still shows /create. Normally the URL would be something like .../#Session/view/<id> after creating or saving a record. Here is my error handler with the irrelevant parts removed:
    Code:
    define('custom:error-handlers/client-unavailable', [], function () {
      return class {
        constructor(view) {
          this.view = view;
        }
    
        process(data) {
          ...
    
          this.view.createView('dialog', 'views/modal', {
            ...
            buttonList: [
              {
                ...
                onClick: dialog => {
                  dialog.close();
    
                  return this.view.save({
                    headers: {'X-Skip-Availability': 'true'}
                  }).then(() => {
                    this.view.actionCancelEdit();
                    this.view.reRender();
                  });
                }
              },
            ...
    });
    Notably, the X-Skip-Availability header is the only way I have figured out how to pass data to the backend. Setting data.options does not work and neither does anything else I've tried. Here is how the backend uses the header:

    PHP Code:
    
    $headers = getallheaders();\
    $skipAvailability = filter_var(
      $headers['X-Skip-Availability'] ?? false,
      FILTER_VALIDATE_BOOLEAN
    ); 
    
    Last edited by bandtank; Today, 01:18 AM.
Working...