Saturday, 27 June 2015

Gravity form tweaks and techniques

Gravity form tweaks

Gravity form -  Multiple form instances on same page

Sometime you need to call gravity form multiple times on the same page but this needs some amendments to the normal flow of gravity form. Here I am going to demonstrate  how you will meet this requirement.

I have used one wp plugin "Gravity Forms: Multiple Form Instances" and done some modifications to facilitate the following requirement :

1- Need to make it work for a single form and leave rest as it is as most of the validation is based on the form ids.
2- In multiple form instance plugin random ids are generated to differentiate same form access as multiple instance. I have made it little simple by using sequential ids rather than random number. Like : Form id - 15 will be accessed over one page as 151, 152,153 and so on. Follow the below steps:

1- Perform below amendment in the function "gform_get_form_filter" definition in plugin file as under.

public static $cnt = 0;
self :: $cnt = self :: $cnt +1;

// If from has been submitted, use the submitted ID, otherwise generate a new unique ID
        if (isset($_POST['gform_random_id'])) {
            $random_id = $_POST['gform_random_id'];
         } else {
            $random_id = $form['id'].self :: $cnt;//mt_rand();

2- Set your form id condition so that code get executed for specific form only. 
if( $form['id'] == 'YOUR_FORM_ID' ) after function definition starts.

Gravity form tabindex conflict

I came to another issue with gravity form. When we use tab in first field and its having conditional logic to open rest fields on the basis of first field input, form focus goes to another form field rather than going to next field within the same form. To overcome this, I have used below code :

add_filter( 'gform_tabindex', 'gform_tabindexer', 10, 2 );

function gform_tabindexer( $tab_index, $form = false ) {
    $starting_index = 100; // Update this number if higher index require
     if( $form )
     add_filter( 'gform_tabindex_' . $form['id'], 'gform_tabindexer' );
     return GFCommon::$tab_index >= $starting_index ? GFCommon::$tab_index : $starting_index;

Gravity form window scrolls on submit

When we submit ajax enabled gravity form, page or window scroll/jump little bit to set it on top focus. To stay form on fixed position use below code :

add_filter( 'gform_confirmation_anchor', '__return_false' );

Gravity form after form submit event handler

Sometime it is needed to do some stuffs after the ajax enabled gravity form submission. Here is the code :

$(document).bind('gform_confirmation_loaded', function( event, formId ){

// perform your required logic here

Thanks for your valuable comments and suggestions

No comments:

Post a Comment

YouTube TV | Know All About Features, Subscription & Access

YouTube TV is Live | Know All About YouTube TV YouTube TV is finally live and available to Big US cities only. Now you ...