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.

Flag that makes it possible to have non numeric $entry[] keys

  1. Hi Carl, you and I traded a few emails last week about forum access and API integrations. As I mentioned in one of those emails, my company does a lot of API integration work. We've just wrapped up a nice set of lead and contact forms that integrate with BatchBook for one customer, and we plan to do more custom integrations with Gravity Forms as the forms platform.

    On to my Feature Request - it would be great if you could add a flag that allows us to have Gravity Forms use non-numeric keys for fields in the $entry[] object/array. I looked through the code and noticed that you're stripping 'input_' off of the field names before inserting them into $entry[].

    We post all form fields out to various services, and I'd prefer to just be able to use http_build_query($entry), but I'm having trouble with the complex field keys. http_build_query() has an option that adds a string prefix to numeric keys (since PHP doesn't like numeric keys inside $_POST[]), but it doesn't catch the complex field key names like '5.1', '5.2', etc.

    This means we need to loop through $entry[] and sanitize the complex field keys before building our query and sending it off into the aether. Would be nice if we could just set a flag that says "don't strip 'input_' off of the front of the $_POST[] key".

    Hope that makes sense :-)

    Posted 13 years ago on Thursday May 5, 2011 | Permalink
  2. I wouldn't mind adding a hook for you to make your life a little easier. Since you seem pretty familiar with Gravity Forms and know exactly what you need, it would help me if you could suggest where this hook should be placed.

    Posted 13 years ago on Sunday May 8, 2011 | Permalink
  3. HI Alex,

    That's a good question. I'm not sure exactly where the hook would need to go, but I'll describe what I'm doing and maybe that'll be enough to help you figure it out.

    Right now we're adding an action on gform_post_submission. Currently we have to sanitize the keys in $entry[] - we're just replacing '.' with '_' in all keys so that a key like '5.1' is turned into '5_1', which makes PHP treat the key as a string instead of a decimal number.

    $cleaned_entry = array();
    	foreach ($entry as $k => $v) {
    		$cleaned_entry[str_replace('.', '_', $k)] = $v;
    	}
    	$post_vars = http_build_query($cleaned_entry, 'f');

    The 'f' in that http_build_query call adds an 'f' in front of any key that is an int.

    So, consider an $entry[] with the following key value pairs:
    1=email@example.com
    2.1=John
    2.2=Doe

    http_build_query($entry[]) with no preprocessing and no 'f' parameter gives us the following querystring:
    1=email@example.com&2.1=John&2.2=Doe

    PHP doesn't like $_POST[] items with numeric keys, so we use the prefix trick, http_build_query($entry[], 'f'), which results in the following querystring:
    f1=email@example.com&2.1=John&2.2=Doe

    Our first key (1) is 'fixed' but PHP doesn't add the 'f' in front of the 2.1 and 2.2 keys, unfortunately. So we need sanitize the entry with the code I showed above, first. Then http_build_query($cleaned_entry[], 'f') returns the following querystring, with all numeric keys replaced by string keys:
    f1=email@example.com&2_1=John&2_2=Doe

    We then drop that string into a cURL post to an external service (also PHP). The external service is happy with the string keys, but doesn't like the numeric keys (neither ints or decimals).

    It's not huge, but it's an extra step that has to be performed by anyone who wants to package up $entry[] and send it off to some external PHP service for additional processing. Would be great if there was a flag that I could set that just says 'don't strip "input_" off of the front of key names in $entry[]', in which case http_build_query($entry[]) would return the following querystring:
    input_1=email@example.com&input_2.1=John&input_2.2=Doe

    Make sense?

    Posted 13 years ago on Tuesday May 10, 2011 | Permalink
  4. It does make a lot of sense if you look at your application. The problem is that other areas of Gravity Forms (notifications, variable replacement, etc...) expect the keys to be in that format, so it might end up being more complex than it looks to add that flag and keep everything else working as it should. A compromise might be to add a function in Gravity Forms that will sanitize the entry, that way, anybody with the same needs you have can just call that function to have the entry sanitized. Even better, we have always wanted to develop an add-on that will post to third party services, allowing you to specify the name for each form field. That way you would be able to "rename" fields a build the post string in a way that is expected by the receiving page.

    Posted 13 years ago on Tuesday May 10, 2011 | Permalink
  5. Adam Cancado: Even better, we have always wanted to develop an add-on that will post to third party services, allowing you to specify the name for each form field.

    Yes. I think this would be a better solution, anyway. Want some help writing it? I'm digging into the code to do just that at the moment. I was thinking of having it try to use the adminLabel, if it exists, the normal label, otherwise. I'd sanitize whichever one we used. Probably use the built-in sanitize_title_with_dashes() WordPress function. I'm pretty confident that would generate valid keys.

    I'm sure you're aware of this, and hindsight's 20/20, but using floats and ints cast as strings for array keys can cause problems in some situations (such as in our use).

    Posted 13 years ago on Tuesday May 10, 2011 | Permalink
  6. Here's what I've got, so far. Three utility functions, plus an example of how to call them from the gform_post_submission hook:

    http://pastebin.com/R92qB6ZW

    Only lightly tested, so far.

    Posted 13 years ago on Wednesday May 11, 2011 | Permalink
  7. What we had in mind was to provide a screen where users could "map" fields to names, similar to what the other add-ons do. That way, you wouldn't have to rely on the labels. It will be very likely that the third party service expects the fields to be named something different than what is in the form label.

    Posted 12 years ago on Thursday May 12, 2011 | Permalink
  8. That'd be great. If you'd like to share your shell plugin with me, I'd be happy to take a look at it.

    The solution I posted allows a user to define admin labels, giving him a little more control over the field names that are sent with the resulting webhooks. I considered making that a bit more customizable, but it may not make sense to try to shoehorn a custom solution in there if you've already got a good, reusable foundation to work with.

    Posted 12 years ago on Friday May 13, 2011 | Permalink