Hi all,
Having a problem with nested repeater fields.
It seems if any of the nested fields are left blank, then it doesn’t save the input data for an subsequent nested fields. Here’s an example form to show what I mean. Here are the steps to recreate the problem:
- Go to http://owen-lewis.com/gf-repeater-test/
- Enter something in the first ‘Element’ field
- Leave the first ‘Fact’ and ‘Evidence for Fact’ fields blank
- Add a new ‘Element’
- Enter some data for all three fields in the new ‘Element’
- Submit the form.
You’ll see that the ‘Fact’ and ‘Evidence’ fields for the second ‘Element’ are not saved in the entry.
Here is my code to create the fields:
// Add repeater fields
function add_fields($form){
$element = GF_Fields::create(array(
'type' => 'text',
'id' => 1001,
'formId' => $form['id'],
'label' => 'Element',
'pageNumber' => 1
));
$fact = GF_Fields::create(array(
'type' => 'text',
'id' => 1003,
'formId' => $form['id'],
'label' => 'Fact',
'description' => 'List a fact that\'s asserted to support the element.',
'pageNumber' => 1
));
$evidence = GF_Fields::create(array(
'type' => 'text',
'id' => 1005,
'formId' => $form['id'],
'label' => 'Evidence for Fact',
'description' => 'What evidence proves or disproves this fact?',
'pageNumber' => 1
));
$evidences = GF_Fields::create( array(
'type' => 'repeater',
'id' => 1004,
'formId' => $form['id'],
'label' => 'Evidence',
'addButtonText' => 'Add evidence',
'removeButtonText' => 'Remove evidence',
'pageNumber' => 1,
'fields' => array($evidence),
));
$facts = GF_Fields::create( array(
'type' => 'repeater',
'id' => 1002,
'formId' => $form['id'],
'label' => 'Supporting Facts',
'addButtonText' => 'Add fact',
'removeButtonText' => 'Remove fact',
'pageNumber' => 1,
'fields' => array($fact, $evidences),
));
$elements = GF_Fields::create( array(
'type' => 'repeater',
'id' => 1000,
'formId' => $form['id'],
'label' => 'Elements',
'addButtonText' => 'Add element',
'removeButtonText' => 'Remove element',
'pageNumber' => 1,
'fields' => array($element, $facts),
));
$form['fields'][] = $elements;
return $form;
}
add_filter('gform_form_post_get_meta_1', 'add_fields');
// Remove fields before the form is saved.
function remove_fields($form_meta, $form_id, $meta_name){
if($meta_name == 'display_meta'){
// Remove the Repeater field: ID 1000
$form_meta['fields'] = wp_list_filter($form_meta['fields'], array('id' => 1000), 'NOT');
}
return $form_meta;
}
add_filter('gform_form_update_meta_1', 'remove_fields', 10, 3);
Any Ideas?
Thanks,
Owen.