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.