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 populate List field

  1. acsnaterse
    Member

    Is it also possible to dynamically pre-populate the "new" List field?

    It's no problem to do this with checkboxes or any other input type, but I can't find the "format" for the "choices" array items of this List field.

    Any help would be appreciated :)

    Posted 12 years ago on Monday November 7, 2011 | Permalink
  2. Can you post a use case for this? It sounds interesting. I will have one of the developers take a look at it.

    Posted 12 years ago on Tuesday November 8, 2011 | Permalink
  3. acsnaterse
    Member

    Well, in this case I want to use it for a wishlist, but I guess you also can use it for a kind e-commerce solution.

    So I have a bunch of products, and I'd like to display them in a GF list, with product name in column A and the quantity in column B. The product names have to be pre-populated (based on the contents of the wishlist). The quantity can be entered by the user.

    As far as I can see the (unserialized) "format" of the list data array (after submitting) is the following:

    Array
    (
        [0] => Array
            (
                [Product] =>
                [Quantity] => 200
            )
    
        [1] => Array
            (
                [Product] => Product 2
                [Quantity] => 400
            )
    
        [2] => Array
            (
                [Product] => Product 3
                [Quantity] => 300
            )
    
    )

    However, I can't use this same format when populating the "choices" array in the gform_pre_render filter. So it would be great if we can :)

    any other solutions are fine too, but I think it's the easiest if the format stays the same.

    One backdraw however: for this method you need to know the exact name of the columns to use it for the array keys. Maybe it's better to just map it to a regular index.

    Posted 12 years ago on Tuesday November 8, 2011 | Permalink
  4. You can pre-populate list field using the gform_field_value_$parameter_name filter.
    Following is a sample snippet that will help you with the data format. This example assumes you used "list" as the parameter name for that field.

    add_filter("gform_field_value_list", "populate_list");
    function populate_list($value){
       return array("row 1 - col1","row 1 - col2", "row 1 - col3",
                    "row 2 - col1", "row 2 - col2", "row 2 - col3",
                    "row 3 - col1", "row 3 - col2", "row 3 - col3");
    
    }
    Posted 12 years ago on Tuesday November 8, 2011 | Permalink
  5. acsnaterse
    Member

    Alex, thanks! This works great!

    Posted 12 years ago on Wednesday November 9, 2011 | Permalink
  6. Thank you for the update. Glad that worked out.

    Posted 12 years ago on Wednesday November 9, 2011 | Permalink

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