Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active October 3, 2020 08:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/227af18d2f4fe6b055da2d8734694c89 to your computer and use it in GitHub Desktop.
Save Shelob9/227af18d2f4fe6b055da2d8734694c89 to your computer and use it in GitHub Desktop.
jQuery(document).on('cf.form.init', function (event, data) {
/**
The object in the variable data has the following keys:
//idAttr - The form's ID attribute
idAttr: form_id,
//formId - The forms's ID
formId: formId,
//state -the form's state object
state: state,
//fieldIds the ids of all field
fieldIds: CFFIELD_CONFIG[instance].fields.hasOwnProperty('ids') ? CFFIELD_CONFIG[instance].fields.ids : []
*/
console.log(data.fieldIds);
});
jQuery( document ).on( 'cf.form.submit', function (event, data ) {
//data.$form is a jQuery object for the form that just submitted.
var $form = data.$form;
//get the form that is submiting's ID attribute
var formId = $form.attr('id');
if ( window.cfstate.hasOwnProperty( formId ) ) {
var state = window.cfstate[formId];
//log a field's value
console.log(state.getState('fld_6009157_1'));
}
});
jQuery(document).on('cf.form.init', function (event, data) {
//IMPORTANT: Change this to your form ID
if ('CF5a5691fadcf80' === data.formId) {
//get state
var state = data.state;
//disable the field to show programmatically created value.
//IMPORTANT- Change "fld_7788423_1" to the ID of the field you want to be changed by this code
jQuery( '#fld_7788423_1' ).prop( 'disabled' );
/** When a field changes, use it's value to set the value of another field **/
//IMPORTANT - Change "fld_5986742_1" to the ID of the field you want to use the value from
state.events().subscribe('fld_5986742_1', function (value, fieldId) {
//Change the value of another field to this field's new value plus 7 percent
//IMPORTANT- Change "fld_7788423_1" to the ID of the field you want to set
var newValue = value * 1.07;
//change state
state.mutateState('fld_7788423_1', newValue);
//update field -- NOTE: This will not be necessary in future versions of Caldera Forms
jQuery('#fld_7788423_1').val(newValue);
});
console.log(data);
}
});
var state = window.cfstate['CF12345678_1'];
jQuery(document).on('cf.form.init', function (event, data) {
//get state
var state = data.state;
//create callback function to record field as being focused, by form and field ID
var callback = function(fieldId,value){
console.log(fieldId);
//report field ID and (if you want value) to Google Analytics
ga('send', {
hitType: 'form',
eventCategory: data.formId,
eventAction: 'focus',
eventLabel: fieldId
});
};
data.fieldIds.forEach( function(fieldId){
state.events().subscribe(fieldId,callback)
});
//report form loaded
ga('send', {
hitType: 'form',
eventCategory: data.formId,
eventAction: 'load',
});
});
@resbraga
Copy link

resbraga commented Nov 7, 2019

I suspect line 16 of field-math.js has its attributes inverted:

state.events().subscribe('fld_5986742_1', function (value, fieldId) {
should be
state.events().subscribe('fld_5986742_1', function (fieldId, value) {

Right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment