We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37108
    • 80 Posts
    I'm working on a plugin to validate the uniqueness of a resource (containing a person's name, addresses, and other info) when saved. I know how to issue an error and stop the process but am looking for a way to create a confirmation dialog to allow saving (with a warning) when records with similar data are found. Any ideas?
      • 3749
      • 24,544 Posts
      The only ways I know involve JavaScript connected to the save button.

      In ExtJS/modExt, it would be:

      MODx.msg.confirm( ...


      In JQuery, you'd need to create a dialog as describe here:

      https://stackoverflow.com/questions/12617084/jquery-confirm-dialog

      You could also use plain JavaScript.

      In any case, you'd want to stop the default action if the user chooses not to proceed, maybe with a "Save Cancelled" message.



        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 37108
        • 80 Posts
        Bob,

        Thanks for the pointers. I'm aware of the modExt method and will go that route. Given that this plugin is checking a normal modResource document, I'm guessing that I'd do this in upd mode (lifted from /manager/assets/modext/sections/resource/update.js):
        MODx.msg.confirm({
            text: 'message text'
            ,url: MODx.config.connector_url
            ,params: {
                action: 'resource/update'
                ,id: this.config.resource
            }
            ,listeners: {
                success: {fn:function(r) {
                    MODx.loadPage('resource/update', 'id='+r.object.id);
                },scope:this}
            }
        });
        


        ... with the only difference in new mode being that the params would be:
        ,params: {
            action: 'resource/create'
        }
        


        Does that look right? The only other question is whether it's better to fire this OnBeforeDocFormSave or OnDocFormSave?
          • 3749
          • 24,544 Posts
          It's a difficult issue. There's no way I know of to have PHP code in MODX pop up a dialog. All you can do is inject JS code into the page that will execute on some page event (like a click). You shouldn't have to save the resource yourself, since MODX is happy to do it for you. The question is how to inject your pop-up into the equation.

          One solution is to catch the process even earlier when the user clicks on the "Save" button. Your JS code can intercept that click and (if the user says no) prevent the default action of the button, or (if the user says Yes) return without doing anything and let MODX continue saving the resource.

          You'd want to have your plugin inject your JS code in OnDocFormRender or OnDocFormPrerender, because it needs to be there before "Save" is clicked. On the other hand, the logic for presenting the confirm button would have to all be in the JS code since the information you need to make that decision won't be there until the "Save" button is actually pressed.

          This would be the OnDocFormRender (or Prerender) code:

          if ($mode == modSystemEvent::MODE_UPD) {
              $modx->regClientStartupScript('path/to/js/file.js');
          }
          
          return ''
          


          That's all the PHP code you'd have.

          I can't think of any way to do it using either of the DocFormSave events since there's no good way to interact with the user in them.



          [ed. note: BobRay last edited this post 5 years, 2 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 37108
            • 80 Posts
            Thanks for putting additional thought into the issue! I've been doing the same after discovering, as you mention, that the JS would have need to be injected in the render phase. It got me wondering whether the Ext.override method could be used to either alter or add to classes/components/methods already established in the manager's create.js and update.js. For what I'm trying to do, clicking save would first have to invoke a custom processor to perform the validation logic, which upon sending success would allow the normal create/update process to continue or call MODx.msg.confirm should user interaction be necessary. If that's plausible, I'm not quite sure of how to go about it, in terms of coding the override.

            All this also has me pondering whether this part of my application — allowing managers to build a library or people and organizations which are used/displayed in various ways — is better built in a CMP or CRC. Without the desire for the finer-grained, multi-level validation I could build this in normal resources and a handful of TVs, using Collections to simplify the display and navigation of the library.
              • 3749
              • 24,544 Posts
              One issue that goes against my suggested method is some MODX extras are already hijacking the resource Save button. I'm pretty sure Collections does. Even if the user says "no" I think Collections is going to save the resource anyway.

              If I were doing it, I think I'd either use NewsPublisher and hijack *its* "Save" button, or (more likely) just create a CMP where you'd have control over everything that happens and wouldn't have to worry about what else is going on.

              Another option would be FormIt2DB which I think could have a postHook that would display your rejection notice (not as a popup, but as an error message). I think FormIt also has the option of custom validators. I could be wrong. I've never done this, so I don't know how practical this suggestion is.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 37108
                • 80 Posts
                While I may ultimately create this contact data management in a CMP, I'm interested in learning how to alter the default manager functionality (in a proper, non-hacky way) and so have continued to research how to manipulate the process via Extjs and custom processors. I've believe the way to go is using overrides inserted during the render phase. Note re Collections: It does prepend the button bar with its own button (to navigate back to a child's collection/selection) but does not appear to alter the CRUD processes otherwise.

                Anyway, now I've got some Ext questions that I'll pose in a new post since it strays a bit from the subject of this thread. Again, thanks for your thoughts on this!