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-select an option based on user data?

  1. Hello all :) The more I use Gravity Forms, the more I'm impressed with it.

    So I have yet another question. Using this tutorial I've managed to pre-populate a form field with custom data enteres in a logged-in users' profile. (Basically, I created some extra user meta fields, and had them popped into the profile, and created a dropdown of choices in a form based on what the logged-in user has in those fields. In this case, it's different addresses. So if the user is logged in, the addresses in his profile are set as the options in the dropdown.)

    Now my question is this: can you create a form with options, and - using information placed in the usermeta - decide if those options are selected by default? the current code I'm using simply rewrites the entire options list, but I dont' want to do that. I just want to change the default selected item to whatever is set in the user's profile (if anything) - and revert to gravity Forms' selected default option if nothing is in t users profile.

    Wow, I hope that make sense.

    As an example, this is the code where I'm pulling data from the profile to create a dropdown list of addresses:

    add_filter('gform_pre_render_32', 'populate_delivery_address_options');
    function populate_delivery_address_options($form) {
    	foreach($form['fields'] as &$field) {
    		if($field['type'] != 'select' || strpos($field['cssClass'], 'delivery_options') === false) continue;
    		$shipping = shipping_array();
    		$choices = array(array('text' => 'Please Choose an Address', 'value' => ' ') );
    		foreach($shipping as $name => $address) {
    			$choices[] = array('text' => $name . ': ' . $address, 'value' => $name . ': ' . $address);
    		}
    		$field['choices'] = $choices;
    	}
    
    	return $form;
    }

    I suspect what I'm looking for is set in the $choices variable, but what I want to do is 1) grab the choices set in Gravity Forms form; 2) compare those choices against what's in the user's data; and 3) set one of the Gravity Form options as "selected" based on what's in the user's data.

    Is this possible, and if so, how would I do that?

    Posted 9 years ago on Wednesday February 1, 2012 | Permalink
  2. Okay! I got it. it's not pretty, but it works. Hope this helps someone else :)

    add_filter('gform_pre_render_32', 'populate_sizing');
    function populate_sizing($form) {
    	global $current_user;
    	$userid = $current_user->ID; // get user ID
    	$usermeta = get_user_meta($userid); // get the user meta for current user
    	$standard = unserialize($usermeta['standard1'][0]); // this field is an array
    	$custom = $usermeta['custom1'][0]; // this field is input text
    
    	foreach($form['fields'] as &$field) {
    		if($field['type'] != 'select' || strpos($field['cssClass'], 'sizing') === false) continue;
    
    		$existing = $field['choices']; // grab the already-exisintg choices from the form
    		$choices = array(); // set up the array
    
    			foreach($existing as $item) {
    				if($standard[0] != ' ') {
    					if($item['text'] == 'Standard') $selected = '1';
    					if($item['text'] != 'Standard') $selected = '';
    				} elseif($custom != '') {
    					if($item['text'] == 'Custom') $selected = '1';
    					if($item['text'] != 'Custom') $selected = '';
    				}
    				// rewrite it so the choice set in the profile is selected and all others are not
    				$choices[] = array('text' => $item['text'], 'value' => $item['value'], 'isSelected' => $selected);
    			}
    
    		// put the rewritten choices back in the form
    		$field['choices'] = $choices;
    	}
    
    	return $form;
    }
    Posted 9 years ago on Wednesday February 1, 2012 | Permalink
  3. tushar
    Member

    Hey,
    I am using buddypress and using gravity forms to fill-up those fields. (All works perfect and It’s really impressive)
    I have created specific user role (teacher, students…etc) on the registration page. Now once user login then- I want different profile field for different type of users.

    This can be possible with buddypress user account type pro (but it is something now working properly),
    http://wpbpshop.com/buddypress-user-account-type-pro

    All I wanted to know – can I redirect user to complete the profile as per their roles with the help of gravity forms.

    Posted 8 years ago on Monday June 3, 2013 | Permalink