PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

What's the best way to pre-populate multiple fields on a single form?

  1. I have one of those "tell-a-friend" kind of forms and I want to default the sender's name and email address with the information stored in their Wordpress profile. I got it working, but it seems like a lot of extra coding when I use one filter/function per field like this:

    Here's how I pre-populate the sender's name:

    add_filter("gform_field_value_f2_fname", "f2_fname");
    function f2_fname() {
    
    	global $current_user;
    	get_currentuserinfo();
    	return $current_user->user_firstname;
    }
    
    add_filter("gform_field_value_f2_lname", "f2_lname");
    function f2_lname() {
    
    	global $current_user;
    	get_currentuserinfo();
    	return $current_user->user_lastname;
    
    }

    When you have multiple forms with the same field parameter names (i.e. fname and lname), I would think that the system would get confused because it wouldn't know which form you are referencing.

    My workaround for this was to pre-pend each parameter name in the form with "f1" for form #1 or "f2" for form #2. Thus, "f2_fname" refers to the "f2_fname" parameter in form #2.

    How can I code the function so that each one lumps all the fields into one function? Perhaps like this:

    add_filter("gform_????", "default_the_fields_on_form_2");
    function default_the_fields_on_form_2($form) {
    
    	global $current_user;
    	get_currentuserinfo();
    	$fname=$current_user->user_firstname;
            $lname=$current_user->user_lastname;
    }

    Thanks,
    Jeff

    Posted 13 years ago on Sunday July 25, 2010 | Permalink
  2. Anyone?

    Posted 13 years ago on Monday July 26, 2010 | Permalink
  3. Jeff,
    Try the following:

    //replace 9 with your form Id
    add_filter("gform_pre_render_9", "populate_fields");
    function populate_fields($form){
    
        global $current_user;
        get_currentuserinfo();
    
        foreach($form["fields"] as &$field)
            switch($field["id"])
            {
                //replace 1 with your first name field id
                case "1":
                    $field["defaultValue"] = $current_user->first_name;
                break;
    
                //replace 2 with your last name field id
                case "2":
                    $field["defaultValue"] = $current_user->last_name;
                break;
            }
    
        return $form;
    }
    Posted 13 years ago on Monday July 26, 2010 | Permalink
  4. This makes total sense thanks!

    Posted 13 years ago on Monday July 26, 2010 | Permalink
  5. Joe T
    Member

    Forgive my ignorance - where would the above code be placed?

    Posted 13 years ago on Sunday August 29, 2010 | Permalink
  6. anointed
    Member

    Wow nice one!
    @DomainWorld it goes in your theme's functions.php file

    Posted 13 years ago on Sunday August 29, 2010 | Permalink
  7. Shauna
    Member

    I am having a tough time figuring out how to set the default value of grouped fields or checkboxes. Can you point me in the right direction.

    I have figured out with grouped fields you can basically assign each a variable name and use the add_filter method to populate but this doesn't seem to work with multi select checkboxes as there is a single variable you can use and you can only check one option.

    Neither of the following are working for me with multi select checkboxes

    -> $VarMultiSelect = $data->value; //parameter name for dynamically pop variable

    -> $form["fields"][40][0]["checked"]=true;

    I know the last is just a stab in the dark but there really isn't any documentation so am basically forced to try various things.

    Thanks for you help

    Posted 13 years ago on Monday March 14, 2011 | Permalink
  8. @Shauna See this forum thread on populating checkboxes:

    http://www.gravityhelp.com/forums/topic/dynamic-checkboxes

    Checkboxes are actually one of the most complex fields to interact with because unlike Drop Downs and Radio Buttons which consist of multiple values, they are really only one input. Checkboxes on the other hand are multiple inputs.

    Posted 13 years ago on Monday March 14, 2011 | Permalink
  9. Shauna
    Member

    @Carl, thanks for the quick reply. If I read the comment correctly, it is possible to display a drop down list dynamically, but it isn't possible to "update", ie, indicate which elements are checked. Is that right?

    "....... It will allow the checkboxes to be added to your form and you will be able to view the entry in the admin detail screen, but you won't be able to change the entry and update the checkbox values."

    Posted 13 years ago on Tuesday March 15, 2011 | Permalink
  10. That is correct. Your code would only change the form display when it is shown to the user. If you edit the entry in the admin, it's going to show the checkboxes values associated with the form configuration itself and not your custom code.

    Posted 13 years ago on Tuesday March 15, 2011 | Permalink