Search code examples
phpwordpressgravity-forms-plugingravityforms

Does Gravity Form gform_pre_submission hook work on forms with Next button and multiple steps?


My form has multiple steps.

You fill out some stuff, click next, and fill out more stuff.

my concern is that the function wont fire until the very end on submit button press.

at that point will I be able to manipulate data on previous fields?

<?php


//Change _6 to the form ID number everywhere
add_action('gform_pre_submission_6', 'capitalize_fields_6');

function capitalize_fields_6($form){

        // add all the field IDs you want to capitalize, to this array

        $fields_to_cap = array('input_id_here');

        // add all uppercase first letter id's, to this array

        $field_to_firstLetter = array('input_id_here');


        foreach ($fields_to_cap as $each) {

                // for each field, convert the submitted value to uppercase and assign back to the POST variable
                // the rgpost function strips slashes

                $_POST[$each] = strtoupper(rgpost($each));
        }

        foreach ($field_to_firstLetter as $each) {

                $_POST[$each] = ucwords(rgpost($each));
        }



        // return the form, even though we did not modify it
        return $form;
}


?>

Solution

  • The gform_pre_submission hook only fires after the form has been actually POSTed, but before anything of consequence has been done with the data from it.

    The multi-page forms don't submit anything between pages, it more or less just wraps the pages in blocks and shows/hides them based - it's just designed as a "more aesthetic" way to present a long form, instead of by having an enormously scrollable form on your page. The exception to this is with the Save & Continue option, but still nothing outside of field masks/formats is actually validated and it's not run through gform_pre_submission.

    If you need to "manipulate the data" on prior pages, you may be better of using JavaScript's .onchange() event handler function to preemptively change the data before it's submitted, but after it's been typed into the fields. You could also use CSS's text-transform property on your desired inputs and set it to capitalize (note this only affects the display and not the actual value, so you'd still need to run it through the gform_pre_submission hook.