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.

Custom Form Field With Multiple Inputs

  1. Hi,

    I am trying to create a form that gets the latitude and longitude from Google maps and saves it to a custom table in WordPress.

    Currently i have created a the required fields and everything populates as expected.

    I have then added a custom field type to gravity forms. This renders fine on the client side however doesn't appear to be working on the server side as it is not sending the values through to gravity forms... i would like the function to display as the name field displays in entries.

    I am hoping you maybe able to let me know where i am going wrong

    // Add a custom field button to the advanced to the field editor
    add_filter( 'gform_add_field_buttons', 'add_loc_field' );
    function add_loc_field( $field_groups ) {
        foreach( $field_groups as &$group ){
            //if( $group["name"] == "advanced_fields" ){ // to add to the Advanced Fields
            //if( $group["name"] == "standard_fields" ){ // to add to the Standard Fields
            if( $group["name"] == "post_fields" ){ // to add to the Standard Fields
                $group["fields"][] = array(
                    "class"=>"button",
                    "value" => __("Add Location", "gravityforms"),
                    "onclick" => "StartAddField('add_loc');"
                );
                break;
            }
        }
        return $field_groups;
    }
    
    // Adds title to GF custom field
    add_filter( 'gform_field_type_title' , 'add_loc_title' );
    function add_loc_title( $type ) {
        if ( $type == 'add_loc' )
    	echo $field["type"];
            return __( 'Add Location' , 'gravityforms' );
    }
    
    // Adds the input area to the external side
    add_action( "gform_field_input" , "add_loc_field_input", 10, 5 );
    function add_loc_field_input ( $input, $field, $value, $lead_id, $form_id ){
    
        if ( $field["type"] == "add_loc" ) {
    
    		$value = array (
    
    		);
            $max_chars = "";
            if(!IS_ADMIN && !empty($field["maxLength"]) && is_numeric($field["maxLength"]))
                $max_chars = self::get_counter_script($form_id, $field_id, $field["maxLength"]);
    
            $input_name = $form_id .'_' . $field["id"];
            $tabindex = GFCommon::get_tabindex();
    
    		//div start
    		$wrap_start = sprintf("<div id='gmaps_location_%s' class='ginput_complex ginput_container'>",$field["id"]);
    
    		//Location - Address
    		$location_address = sprintf("<input type='text' name='input_%d_address' id='%s_address'/><label for='add_loc_" . $field["id"] . "_address' id='add_loc_" . $field["id"] . "_address_label'>Enter Location</label>"	, $field["id"], 'add_loc_'.$field['id'], $location_value, $disabled_text, $field_id);
    
    		//Submit Location
    		$sub_id = "add_loc_".$field['id']."_address";
    		$sub_location = "<input type=\"button\" onclick=\"findAddress(document.getElementById('{$sub_id}').value);\"  value=\"Set Address on Map\" class=\"b_submit\">";
    
    		$location_submit = $sub_location;
    
    		//Map Canvas
    		$map_canvas = "<div id='map_canvas' style='width:500px; height:500px'></div>";	
    
    		//Lat Position
    		$lat_position = sprintf("<input type='text' name='input_%d_lat' id='%s_lat' value='%F' $tabindex %s/><label for='add_loc_" . $field["id"] . "_lat' id='add_loc_" . $field["id"] . "_lat_label'>Latitude</label>", $field["id"], 'add_loc_'.$field['id'], esc_attr(stripslashes($geo_latitude)), $disabled_text, $field_id);
    
    		//Lon Position
    		$lon_position = sprintf("<input type='text' name='input_%d_lon' id='%s_lon' value='%F' $tabindex %s/><label for='add_loc_" . $field["id"] . "_lon' id='add_loc_" . $field["id"] . "_lon_label'>Longitude</label>", $field["id"], 'add_loc_'.$field['id'], esc_attr(stripslashes($geo_longitude)), $disabled_text, $field_id);
    
    		//end div
    		$wrap_end = "</div>";
    
    		$multifield = $wrap_start;
    		$multifield .= $location_address;
    		$multifield .= $location_submit;
    		$multifield .= $map_canvas;
    		$multifield .= $lat_position;
    		$multifield .= $lon_position;
    		$multifield .= print_r($value);
    		$multifield .= $wrap_end;
    
    		//$multifield = sprintf("<div class='ginput_container'><input type= 'text' name='input_%s_address' id='%s_address' class='textfield gform_add_loc %s' value='%s' /></div>", $field["id"], 'add_loc-'.$field['id'] , $field["type"] . ' ' . esc_attr($field['cssClass']) . ' ' . $field['size'] , esc_attr(stripslashes($value)));
    
    		 return $multifield;
    
        }
    
        return $input;
    }
    
    // Now we execute some javascript technicalitites for the field to load correctly
    add_action( "gform_editor_js", "wps_gform_editor_js" );
    function wps_gform_editor_js(){
    ?>
    
    <script type='text/javascript'>
    
        jQuery(document).ready(function($) {
            //Add all textarea settings to the "add_loc" field plus custom "add_loc_setting"
            // fieldSettings["add_loc"] = fieldSettings["textarea"] + ", .add_loc_setting"; // this will show all fields that Paragraph Text field shows plus my custom setting
    
            // from forms.js; can add custom "add_loc_setting" as well
            fieldSettings["add_loc"] = ".label_setting, .description_setting, .admin_label_setting, .size_setting, .default_value_textarea_setting, .error_message_setting, .css_class_setting, .visibility_setting, .add_loc_setting"; //this will show all the fields of the Paragraph Text field minus a couple that I didn't want to appear.
    
            //binding to the load field settings event to initialize the checkbox
            $(document).bind("gform_load_field_settings", function(event, field, form){
                jQuery("#field_add_loc").attr("checked", field["field_add_loc"] == true);
                $("#field_add_loc_value").val(field["add_loc"]);
            });
        });
    
    </script>
    <?php
    }
    
    // Add a custom setting to the add_loc advanced field
    add_action( "gform_field_advanced_settings" , "add_loc_settings" , 10, 2 );
    function add_loc_settings( $position, $form_id ){
    
        // Create settings on position 50 (right after Field Label)
        if( $position == 50 ){
        ?>
    
        <li class="add_loc_setting field_setting">
    
            <input type="checkbox" id="field_add_loc" onclick="SetFieldProperty('field_add_loc', this.checked);" />
            <label for="field_add_loc" class="inline">
                <?php _e("Disable Submit Button", "gravityforms"); ?>
                <?php gform_tooltip("form_field_add_loc"); ?>
            </label>
    
        </li>
        <?php
        }
    }
    
    //Filter to add a new tooltip
    add_filter('gform_tooltips', 'wps_add_add_loc_tooltips');
    function wps_add_add_loc_tooltips($tooltips){
       $tooltips["form_field_add_loc"] = "<h6>Disable Submit Button</h6>Check the box if you would like to disable the submit button.";
       $tooltips["form_field_default_value"] = "<h6>Default Value</h6>Enter the Add Location here.";
       return $tooltips;
    }
    
    // Add a script to the display of the particular form only if add_loc field is being used
    add_action( 'gform_enqueue_scripts' , 'wps_gform_enqueue_scripts' , 10 , 2 );
    function wps_gform_enqueue_scripts( $form, $ajax ) {
        // cycle through fields to see if add_loc is being used
        foreach ( $form['fields'] as $field ) {
            if ( $field['type'] == 'add_loc' ) {
    			wp_register_script( 'gmaps_api', 'http://maps.googleapis.com/maps/api/js?sensor=true');
        		wp_enqueue_script( 'gmaps_api' );
                break;
            }
        }
    }
    Posted 12 years ago on Monday January 2, 2012 | Permalink
  2. Explain what you mean that it works on the client side, not the server side. What does that mean? Is the display of the submitted data missing from the entry view?

    Thank you.

    Posted 12 years ago on Tuesday January 3, 2012 | Permalink
  3. HI Chris,

    Thanks for your reply,
    yeah that is exactly what is happening.

    It displays fine in the browser, but doesn't seem to pass the information to the entries layout in the admin.

    Posted 12 years ago on Tuesday January 3, 2012 | Permalink
  4. I, too, would be interested in the steps necessary to correctly create and integrate my own *custom* Advanced Fields into Gravity Forms.

    The *custom* Advanced Field would contain the following elements: Firstname, Lastname, 2 input fields, birthdate, days-of-the-week single-select drop-down, and time. Then would like to be able to create conditional checkbox(s) that would display/hide instances of this object.

    Any feedback or tutorials would be great. So far, the only example I can find is this posting and from WPSmith, http://wpsmith.net/2011/plugins/how-to-create-a-custom-form-field-in-gravity-forms-with-a-terms-of-service-form-field-example/.

    Best...

    Posted 12 years ago on Tuesday January 3, 2012 | Permalink
  5. Unfortunately, this post was closed, http://www.gravityhelp.com/forums/topic/extending-gravityform, and it no longer open to replies. The above example from WPSmith is a good start, but it does not explain how to create an Advanced Field type with more than one element as I described in my previous comment. Any further examples/insight would be more than helpful.

    Additionally, as a feature request, the ability to create/edit an XML document that contained definitions of your Advanced Field types would be hightly beneficial because then people could create/edit their own Advanced Field types without having to become highly familiar with PHP and JQuery and the nuances of the internals of Gravity Forms. This would allow users to apply limitless combinations of form types to Gravity Forms and extend it's usefulness.

    Best...

    Posted 12 years ago on Tuesday January 3, 2012 | Permalink
  6. Did you ever make progress on this?

    Posted 12 years ago on Monday April 23, 2012 | Permalink
  7. Friend, there's an error at line 11, instead "onclick" => "StartAddField('add_loc'), put onclick" => "StartAddField('addloc'); without the underline character at "addloc"... Obviously, all "add_loc" reference (type) will not work, so I suggest you to substitute every "add_loc" therm to "addloc".

    Posted 11 years ago on Friday May 24, 2013 | Permalink