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.

Pre-Render Names into Form

  1. So I have a form that people register with where they enter their first and last name. Then I have a separate form that they can edit once they're logged in that has different / more data. But it also includes their first and last name field. I need to populate that field with the user's first and last name and (for reasons I'm not going to go into here... just trust me... ) can't use the normal conventions to do so.

    Therefore I have this code... which works for another field but I can't get it to work on the first and last name field. I'm thinking one of the parameters is defined wrong, but I can't figure out which one. Help?

    //Display User Name #
    //form # = the "Personal Profile" form (2)
    add_filter("gform_pre_render_2", "personal_info");
    function personal_info($form) {
    
        if($form["id"] != 2)
           return $form;
    
    	global $post;
    	global $current_user;
    
    	$first_name =  $current_user->first_name;
    	$last_name = $current_user->last_name;
    
    	foreach($form["fields"] as &$field) {
    		if ($field["id"] == 1.3) {
    			$field["defaultValue"] =  $first_name;
    		}
    		if ($field["id"] == 1.6) {
    			$field["defaultValue"] =  $last_name;
    		}
    	}
    	return $form;
    }

    The $first_name is grabbing the name. btw.
    I can test it with a hardcoded string and it still won't work.

    Posted 12 years ago on Wednesday July 18, 2012 | Permalink
  2. The name input is a complex field and as such will have inputs. Please dump the $form array and take a look. Additionally, this is documented here:

    http://www.gravityhelp.com/documentation/page/Gform_pre_render

    Check the second (final) example on that page, specifically where it talks about "complex field" and "inputs." I think you'll get it after that.

    Posted 12 years ago on Wednesday July 18, 2012 | Permalink
  3. Nope... no luck yet.

    This is what I have now. It's exporting the first name to the title of the entire form set (so it says, "Name (Noel)"

    foreach($form["fields"] as &$field) {
    	if(rgar($field, "inputs")) {
    		foreach($field["inputs"] as $input){
    			if ($field["id"] == 1) {
    				$field["inputs"][3]["label"] = $first_name;
    			}
    			if ($field["id"] == 1) {
    				$field["inputs"][6]["label"] = $last_name;
    			}
    		}
    	}
    }
    Posted 12 years ago on Wednesday July 18, 2012 | Permalink
  4. Never mind... I just did it another way.

    I created two single text fields, used a merge tag of {user:first_name} and {user:last_name} to insert the names... then did an "after submission function to update the username based on those fields like so...

    function gf_profile_update( $entry, $form )
    {
    	global $current_user;
    	get_currentuserinfo();
    
    	$first_name = $entry['45'];
    	$last_name = $entry['46'];
    
    	update_user_meta( $current_user->ID, 'first_name', $first_name );
    	update_user_meta( $current_user->ID, 'last_name', $last_name );
    }
    
    add_action( 'gform_after_submission_2', 'gf_profile_update', 10, 2 );
    Posted 12 years ago on Wednesday July 18, 2012 | Permalink
  5. There were a couple problems with your code in this comment but that is a moot point now. It's sometimes easier to get the values from simple, separate form fields as you've done. I'm happy to hear you worked it out. Thanks for the update.

    Posted 12 years ago on Friday July 20, 2012 | Permalink

This topic has been resolved and has been closed to new replies.