New Community Forums are coming. Watch this space for news.
Subscribe: RSS
  • I'm interested in adding validation logic to test for uniqueness of records depending on their template and a certain set of fields to test against. The records in question contain data for contacts of two types—individuals and organizations—each having typical contact fields set up as TVs. The validation needs to prompt for a confirmation when a similar (enough) record is found to that which is being added/updated. For instance, a manager might attempt to create a resource for an individual with the same name but different email address as an existing one. In this case, I'd like to trigger a confirmation dialog saying something like:
    "A record with similar information was found: [firstname] [lastname] [email]. If you're sure the record you're saving is unique, click OK. Otherwise, click cancel."

    What I've arrived at as a possible solution is to override the MODx.page.CreateResource and MODx.page.UpdateResource classes to add a handler to the save button to call a custom validation method. What I'm stuck on is figuring out the code needed to continue the normal process should the user press "OK." Here's an outline of the create override:

    (function() {
    	
    	var origGetButtons = MODx.page.CreateResource.prototype.getButtons;
    
    	Ext.override(MODx.page.CreateResource, {
    	    
    	    getButtons: function (cfg) {
    	    	var items = origGetButtons.apply(this, arguments);
    	    	items[0].handler = this.validateContact;
    	    	return items;
    	    },
    	    
    	    validateContact: function () {
    	    	// Ajax request to connector/processor with validation php, returning 'unique' or 'confirm' (with additional data)
    	    	if(unique){
    	    		// code to continue process???
    	    	} else {
    				Ext.Msg.confirm('Validation Message','The record you are saving may be a duplicate. Continue?',function(e) {
    	                if (e == 'yes') {
    	                    // code to continue process???
    	                } else {
    						// not sure if anything is needed here if staying on the current create/edit page
    	                }
    	            },this);
    			}
    	});
    })();
    


    For reference the MODx.page.CreateResource subclass (from modext/sections/resource/create.js):

    MODx.page.CreateResource = function(config) {
        config = config || {};
        Ext.applyIf(config,{
            url: MODx.config.connector_url
            ,formpanel: 'modx-panel-resource'
            ,id: 'modx-page-update-resource'
            ,which_editor: 'none'
            ,action: 'resource/create'
        	,buttons: this.getButtons(config)
            ,components: [{
                xtype: config.panelXType || 'modx-panel-resource'
                ,renderTo: config.panelRenderTo || 'modx-panel-resource-div'
                ,resource: 0
                ,record: config.record
                ,publish_document: config.publish_document
                ,show_tvs: config.show_tvs
                ,mode: config.mode
                ,url: config.url
            }]
        });
        MODx.page.CreateResource.superclass.constructor.call(this,config);
    };
    Ext.extend(MODx.page.CreateResource,MODx.Component,{
        getButtons: function(cfg) {
            var btns = [];
            if (cfg.canSave == 1) {
                btns.push({
                    process: 'resource/create'
                    ,reload: true
                    ,text: _('save')
                    ,id: 'modx-abtn-save'
                    ,cls:'primary-button'
                    ,method: 'remote'
                    ,keys: [{
                        key: MODx.config.keymap_save || 's'
                        ,ctrl: true
                    }]
                });
    
            }
            btns.push({
                text: _('cancel')
                ,id: 'modx-abtn-cancel'
            });
            btns.push({
                text: _('help_ex')
                ,id: 'modx-abtn-help'
                ,handler: MODx.loadHelpPane
            });
            return btns;
        }
    });
    Ext.reg('modx-page-resource-create',MODx.page.CreateResource);
    


    My sense is there's a simple solution here...any pointers are much appreciated!