Adding an Address type field in a repeater field yields wonky results

In my code, I have a function like this to add a repeater field, and within that repeater field, one of them is an address type. I’ve commented it out for now.

/**
 * Create Repeater Field
 *
 * @see /wp-admin/admin.php?page=gf_edit_forms&id=2
 * @see https://docs.gravityforms.com/gform_form_post_get_meta/
 *
 * @param object $form The form object
 *
 * @return object The modified form object.
 */
function au_form_field_add_repeater($form)
{
    $_fields = array_column($form['fields'], 'inputName');
    // Insert me at/after my placeholder
    $index = array_search(
        'repeater_placeholder',
        $_fields
    );
    if ($index < 0) {
        return $form; // Bail, doesn't exist
    }

    $form_id = 2;
    $field_id = 1100; // Arbitrarily & absurdly high number so it is unlikely to be used

    // Create the subfields within the repeater field
    $fields = array(
        array(
            'label' => __('Full Name'),
            'type' => 'name',
        ),
        array('label' => __('Relationship')),
        array(
            'label' => __('Date of Birth'),
            'type' => 'date',
            'dateFormat' => 'mdy',
            'dateType' => 'datedropdown'
        ),
        // array(
        //     'label' => __('Mailing Address'),
        //     'type' => 'address',
        //     'addressType' => 'us',
        // ),
        array('label' => __('Email Address'), 'type' => 'email'),
        array(
            'label' => __('Phone'),
            'type' => 'phone',
            'phoneFormat' => 'standard'
        ),
        array(
            'label' => __('Percentage'),
            'type' => 'number',
            'rangeMax' => 100,
            'rangeMin' => 0
        ),
    );
    foreach ($fields as $i => $field) {
        $fields[$i] = GF_Fields::create(array_merge(array(
            'type' => 'text',
            'id' => $field_id + $i, // The Field ID must be unique on the form
            'formId' => $form_id,
            'label' => '',
            'pageNumber'  => 1,
            'size' => 'large',
            'cssClass' => 'gfield--width-half'
        ), $field));
    }

    // Create the repeater field
    $repeater = GF_Fields::create(array(
        'type' => 'repeater',
        'id' => $field_id,
        'formId' => $form_id,
        'label' => '', // Blank,
        'pageNumber' => 1,
        'descriptionPlacement' => 'above',
        'fields' => $fields // Add the fields here.
    ));

    // Add the repeater field to the form at the placeholder index
    array_splice($form['fields'], $index, 0, array($repeater));

    return $form;
}
add_filter('gform_form_post_get_meta_2', 'au_form_field_add_repeater');

Now when I view my form on the frontend, it looks great. At the spot of my placeholder field (which is just a hidden field with its visibility set to “Administrative” and has the box checked to allow it to be prepopulated and then it’s paramter name is repeater_placeholder), I see a single instance of an empty group of my fields (which I like to call the “starter pack” for repeater fields) with the “add” button below it. Great!

However, as soon as I uncomment the lines to add the address type field to my repeater, instead of one starter pack, I get over 1k starter packs on the frontend! What gives? Why does this field type break the repeater?

Any help is appreciated. I tried reaching out to tech support but I don’t have access to the client’s account to actually submit a proper ticket.

The arrays defining the properties for the name and address fields are missing the inputs property.

Those field types use multiple inputs, so you need to define them in the array or the field markup won’t be generated correctly. See their documentation pages for examples of what those properties should contain:

1 Like

Thank you! I had assumed they would take the defaults if omitted. For completion’s sake, the snippet for my mailing address field now looks like this in the array:

array(
	'label' => __('Mailing Address'),
	'type' => 'address',
	'addressType' => 'us',
	'defaultCountry' => 'United States',
	'hideCountry' => true,
	'inputs' => array(
		array('id' => "{$field_id}.1", 'name' => 'street', 'label' => __('Address Line 1')),
		array('id' => "{$field_id}.2", 'name' => 'address2', 'label' => __('Address Line 2'), 'isRequired' => false), // Always optional
		array('id' => "{$field_id}.3", 'name' => 'city', 'label' => __('City')),
		array('id' => "{$field_id}.4", 'name' => 'state', 'label' => __('State')),
		array('id' => "{$field_id}.5", 'name' => 'zip', 'label' => __('Zip Code')),
		array('id' => "{$field_id}.6", 'name' => 'country', 'label' => __('Country'), 'isHidden' => true),
	),
),

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.