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.

drop down value different than content text

  1. Erwin van den Boogaard
    Member

    Hi guys,

    Since I need to pass a country abbreviation (and not the country name) to a 3rd party script, I need to be able to modify the country values.
    So instead of:
    <option value='United States' >United States</option>
    I need:
    <option value='US' >United States</option>

    Carl wrote 4 months ago: "Right now it is not possible to create a drop down with a value different than the content text. But we do plan on adding this feature in an upcoming release. "

    Is it already possible? If not yet, how can I hack the values?

    Thanks,
    Erwin

    Posted 12 years ago on Tuesday February 9, 2010 | Permalink
  2. Jan Egbert
    Member

    This should do the trick. You should put this in your theme's functions.php.

    // Replace the 7 to desired form ID
    add_filter( 'gform_pre_render_7', 'populate_dropdown' );
    
    function populate_dropdown($form){
    
        //Creating drop down item array.
        $items = array();
    
        //Adding initial empty value
        // $items[] = array("text" => "", "value" => "");
    
        //Adding countries as items
        $items[] = array("text" => "United States", "value" => "US");
        $items[] = array("text" => "Germany", "value" => "DE");
        $items[] = array("text" => "Italy", "value" => "IT");
    
        //Adding items to field id 1. Replace 1 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 1){
                $field["type"] = "select";
                $field["choices"] = $items;
            }
    
        return $form;
    }

    Uncomment // $items[] = array("text" => "", "value" => ""); if you want to add an empty initial value.

    Posted 12 years ago on Tuesday February 9, 2010 | Permalink
  3. Erwin van den Boogaard
    Member

    That works like a charm.
    Thanks (Dank je wel) Jan Egbert!

    Cheers,
    Erwin

    Posted 12 years ago on Tuesday February 9, 2010 | Permalink
  4. Erwin van den Boogaard
    Member

    Next step is how can I preselect one of those dynamic values in that drop down that I build dynamically with your code?
    For example: user record shows NL as country, so the drop down should show "Netherlands".

    Thanks,
    Erwin

    Posted 12 years ago on Thursday February 11, 2010 | Permalink
  5. How do you need to set the preselect? Via a query string parameter or using PHP?

    Posted 12 years ago on Thursday February 11, 2010 | Permalink
  6. Erwin van den Boogaard
    Member

    By using PHP.

    Posted 12 years ago on Thursday February 11, 2010 | Permalink
  7. Typically you would use PHP in your themes function.php to pre-select it...

    - Edit the field
    - Select Advanced Tab
    - Check the "Allow field to be populated dynamically" checkbox
    - Give the field a parameter name

    Place the code in your function file...

    Ex.

    add_filter("gform_field_value_city", "populate_city");
    function populate_city(){
    $city_value = "New York";
    return $city_value;
    }

    However i'm not 100% sure if it will work when you are pre-populating the actual values using custom code.

    BUT you can certainly try doing it this way and place the filter above to pre-select the drop down item BELOW the code where you populate the drop down... that might work. Let me know how it goes.

    Posted 12 years ago on Thursday February 11, 2010 | Permalink
  8. Just to confirm, Carl's solution above will work.

    Posted 12 years ago on Thursday February 11, 2010 | Permalink
  9. Erwin van den Boogaard
    Member

    Sorry to say, but it didn't work :(

    I have this to populate the drop down (this works)

    add_filter( 'gform_pre_render_1', 'populate_dropdown' );
    function populate_dropdown($form){
        $items = array();
        $items[] = array("text" => "Canada", "value" => "CA");
        $items[] = array("text" => "France", "value" => "FR");
        $items[] = array("text" => "Germany", "value" => "DE");
        $items[] = array("text" => "Netherlands", "value" => "NL");
        $items[] = array("text" => "United Kingdom", "value" => "GB");
        $items[] = array("text" => "United States", "value" => "US");
    
        foreach($form["fields"] as &$field)
            if($field["id"] == 6){
                $field["type"] = "select";
                $field["choices"] = $items;
            }
        return $form;
    }

    Now, to pre-select the drop down the following code SHOULD work, but it doesn't:

    add_filter("gform_field_value_country", "populate_country");
    function populate_country(){
    $country_value = 'US'; // for testing purposes I hard-coded setting the value
    return $country_value;
    }

    But what SHOULD NOT work, but did work is the following:

    add_filter("gform_field_value_country", "populate_country");
    function populate_country(){
    $country_value = 'United States';  // for testing purposes I hard-coded setting the value
    return $country_value;
    }

    Unfortunately I only have the real value (US, UK, NL, etc.) to select the correct one.

    So here's what did work: I added the following after
    $items[] = array("text" => "United States", "value" => "US");

    foreach($items as &$country )
    		if ($country["value"] == 'US' {
    			$country = array("text" => $country["text"], "value" => $country["value"], "isSelected"=> true);
    		}

    Note: I'm not a PHP coder, I'm a code thief ;) so some copy-pasting and adding some common sense/logic takes me a long way. I did get the result I wanted BUT what I don't know is if the code will bring a heavy strain on the server or wreck my Gravity form in any way.

    So can you please check if my code gets your OK?

    Thanks,
    Erwin

    Posted 12 years ago on Thursday February 11, 2010 | Permalink
  10. Erwin,
    Your code looks good (technically you don't need the second loop to set the "isSelected" property. You can just set it along with the "text" and "value" when building the $items[] array, but it shouldn't cause any problems as is. Good job hacking Gravity Forms! Using the "value" property of drop down items is something we are planning on incorporating into Gravity Forms, but haven't fully implemented it yet.

    The only possible drawback I can think of is that the country stored in the database will be the abbreviation. So if you send notification to users, the abbreviation will be sent. This may or may not be ok.

    Posted 12 years ago on Friday February 12, 2010 | Permalink
  11. Erwin van den Boogaard
    Member

    Hi Alex,

    How do you mean 'technically you don't need the second loop to set the "isSelected" property. You can just set it along with the "text" and "value" when building the $items[] array'?
    Wouldn't i need to check the country_id if it matches? Or do you mean that i could include the 'if' while filling the array, like this:
    $items[] = array("text" => "Canada", "value" => "CA", if ($country["value"] == 'US') "isSelected"=> true);

    Regarding the storage of the country in the database: it would be great if both the text and value of drop down could be accessed independently, like: {country(text):6}, {country(value):6} (just thinking out loud here ;) )

    Thanks,
    Erwin

    Posted 12 years ago on Friday February 12, 2010 | Permalink
  12. Jan Egbert
    Member

    Try the snippet below. Not sure if it's the most effecient method. But it seems to work fine.

    add_filter("gform_pre_render_4", "populate_and_preselect_country");
    function populate_and_preselect_country($form){
    
        $mycountries = array();
    
    	$mycountries[] = array("name" => "Canada", "value" => "CA");
        $mycountries[] = array("name" => "France", "value" => "FR");
        $mycountries[] = array("name" => "Germany", "value" => "DE");
        $mycountries[] = array("name" => "Netherlands", "value" => "NL");
        $mycountries[] = array("name" => "United Kingdom", "value" => "GB");
        $mycountries[] = array("name" => "United States", "value" => "US");
    
        $items = array();
    
        foreach($mycountries as $mycountry){
        	$country = $_GET["country"];
            $is_selected = $mycountry["value"] == $country ? true : false;
            $items[] = array("value" => $mycountry["value"], "text" => $mycountry["name"], "isSelected"=> $is_selected);
        }
    
        foreach($form["fields"] as &$field)
            if($field["id"] == 1){
                $field["type"] = "select";
                $field["choices"] = $items;
            }
    
        return $form;
    }
    Posted 12 years ago on Friday February 12, 2010 | Permalink
  13. Erwin van den Boogaard
    Member

    Alex wrote you don't need the second loop to set the "isSelected" property, but you seem to do the same as I did two posts ago ;)
    What's this different than the code that I came up with?

    Posted 12 years ago on Friday February 12, 2010 | Permalink
  14. FYI- if you want to set isSelected this way, you have to turn off the "Allow field to be populated dynamically" option to avoid GF overriding the manually set selection -- at least that's what I had to do to get my form working.

    This came in very handy when I needed to populate a drop down with a list of custom post types!

    Posted 11 years ago on Thursday August 12, 2010 | Permalink
  15. Thanks Jan, works like a charm!

    Posted 11 years ago on Saturday January 8, 2011 | Permalink

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