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.

How To: Custom Field Values Without Having to Use IDs

  1. Something like this may be posted elsewhere, but as I haven't found it, figured I'd post in case it helps someone else.

    I needed to use custom values for some drop down menus on a form I'm doing for a client. This is my first time using Gravity Forms and I found here in the forums that you have to do a bit of coding to accomplish this -- the built in functionality is to make the value and the label of a field be the same. So if you need option 1 value='1" and the displayed value = First Option, you've go to do some work.

    The solution posted here in the forums requires you to get the field ID of the field you want to use custom values for by looking at the source code of the form. No big deal if you're only modifying one or two fields, but I have the drop down repeated potentially up to 10 times on the form.

    The work around I came up with is to add a custom css class to the form field. In this case, I have a dropdown menu that I gave a css class of "customvalues". I then use that css class to access the fields and use the custom values:


    add_filter('gform_pre_render_1', 'fieldvals');
    function fieldvals($form){
    items = array();
    $items[] = array('text' => "-Displayed Value 1", "value" => '10');
    $items[] = array('text' => "Displayed Value 2", "value" => '20');
    $items[] = array('text' => "Displayed Value 3", "value" => '30');
    foreach($form["fields"] as &$field)
    if($field["cssClass"] == "customvalues"){
    $field["type"] = "select";
    $field["choices"] = $items;
    }
    return $form;
    }

    If you want to do any dynamic updating on the form, you can then access those dropdowns using jquery and the same css class, like so :


    jQuery(document).ready(function($jq) {
    $jq(".customvalues select").change(function(){
    $jq(".customvalues option:selected").each(function(i){
    alert($jq(this).val());
    });
    });
    });

    I'm using it to dynamically calculate totals based on membership options selected on a member sign up form and display the running total. The example above just alerts the value of the selected options.

    Posted 14 years ago on Tuesday February 16, 2010 | Permalink
  2. Very nice. It's great to see people thinking outside the box and leverage some of the capabilities the plugin has from a customization standpoint using hooks with php and jquery.

    We do have plans to add value/label capabilities as well as field calculations in the future.

    Just remember that Gravity Forms is still in it's infancy, we still have a long journey ahead of us as we continue to add new features and improve existing features.

    Thanks for sharing! If you would be willing to write this up as a guest blog post, give some background to what you are doing and write up a detailed blog post/tutorial, we would be glad to post it on the blog and make it available to users who want to read up on how to do this.

    Posted 14 years ago on Tuesday February 16, 2010 | Permalink