Declaring Global variables in form_x.js

ontarget

Active Member
Hi I have a bunch of validations running through form_1.js
Since upgrading to joomla 3.9.x and Fabrik 3.9 i seem to be having issues with js validations even basic onchange show / hide stuff
I'm thinking that my js file is rather messy with form elements being repeated in various functions - I would like to tidy up my work and make all the repeated variables used locally global instead - I think i then have a better chance of fixing my validation issues.
Is this the correct method?

JavaScript:
var modeTransport = Fabrik.getBlock('form_1').formElements.get('aaa_participant_claim___mode_of_transport');
var publicTransport = Fabrik.getBlock('form_1').formElements.get('aaa_participant_claim___public_transport');
var stationEircode = Fabrik.getBlock('form_1').formElements.get('aaa_participant_claim___station_eircode');

function showPublic(){

if (modeTransport.getValue() === 'car') {
publicTransport.hide();
        stationEircode.hide();
  }
}

My old javascript was

JavaScript:
function showPublic(el){
       var elTransport2 = el.form.formElements.get('aaa_participant_claim___mode_of_transport');
    var elPublic2 = el.form.formElements.get('aaa_participant_claim___public_transport');
  
    var elEircodeStat = el.form.formElements.get('aaa_participant_claim___station_eircode');
  
     if (elTransport2.getValue() === 'car') {
         elPublic2.hide();
        
        elEircodeStat.hide();
    }
}
 
Your new way probably won't work, because the form blocks and elements object probably win't have finished initializing before you call Fabrik.getBlock() .... remember that Javascript is asyncronous.

Your old method is perfectly fine, really doesn't matter having el.form.formElements.get() repeated in different functions.

Have you tried sticking breakpoints in your code in the browser dev tools to get a sense of what's going on?

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top