The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

How to add a new entry programmatically in a repeating section?

Labels

This Discussion is public

Notifications

Hello all,

I wonder if someone know how to do that (I haven't found any clue in the official Formidable documentation):

I have a form (A) with a repeating section field (SF) owning a repeated text field (TF). I simply need to add a new TF  entry in the repeating section programmatically (actually when another form (B) entry is created, so I use the 'frm_after_create_entry' action hook for that).

I already do this kind of things easily for NOT-repeating fields, and I didn't imagine it would be complicated to do it for repeating fields, but it is actually! The database structure for such repeating fields is quite complex, and I haven't succeeded in crafting the code yet.

Does anyone know how to do that? Thanks in advance!

Yves.

Did you see this: https://formidableforms.com/knowledgebase/frm_after_create_entry/#kb-create-entry-in-form-with-repeating-section

Hi @vfontjr,

Yes, I had seen this article, thanks a lot. What is unclear to me in this article is that the starting point to fill in the repeating field is a repeating field itself. Thus it is shown how to loop through this initial field to create a new one. But I have unable to understand from this example how to create a brand new repeating field entry.

 

Creating an entry in the second form's repeating field requires passing an array of field values to the second form. In the code this is the $repeating_values array used in the create function.

Whatever data you're passing in from the first form, has to follow this pattern. Every value you need to pass to the second form is contained in PHP's global $_POST variable that you can access in frm_after_create_entry. $_POST is an associative array that contains a lot of details. I recommend you use the Kint debugger to view the actual data your form's $_POST variable contains. There are a couple of Kint plugins in the WordPress repository. I prefer Tonya Mork's.

The basic process is to take each of the individual $_POST['item_meta'] values you need for the repeater, add them to your $repeating_values array, and create the entry in the second form.

Many thanks @vfontjr for the tips! I will try your approach.

I looked into the article in details but it applies to the creation of brand new entries only. In my case, I just want to update an existing repeated field in a repeating section, not to create a new one.

So I investigated the FrmEntry and FrmEntryMeta classes and I ended up with this code that seems to work for me. I share it here in case it might help someone else:
// add a new value to a repeated field in a repeating section of an entry
// - $entry_id = the entry to be updated
// - $form_id = the form of this entry
// - $repeating_section_id = the repeating section inside this form
// - $repeated_field_id = the repeated field inside this repeating section
// - $new_value = the new value to add to this repeated field in the entry
// - $child_form_id = the repeating section child form of the main form

// First we create a new entry with the new value in the repeated field
$row = FrmEntry::create(array(
'frm_user_id' => $user_ID,
'form_id' => $child_form_id,
'parent_item_id' => $entry_id,
'item_meta' => array( $repeated_field_id => $new_value ),
'name' => '' // optionally add a name here
));

// And we add this new entry to the repeating section
if ( $row )
{
// get array of current rows in repeating section
$getrows = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $repeating_section_id );
if ( $getrows )
{
$getrows[] = $row; // add the new row to the array
FrmEntryMeta::update_entry_meta($entry_id, $repeating_section_id, null, $getrows); // update repeating section
}
else
{
$getrows[] = $row; // create a row in the array
FrmEntryMeta::add_entry_meta($entry_id, $repeating_section_id, null, $getrows); // update repeating section
}
}
Many thanks @vfontjr to point me in the right direction!
Yves.

I'm glad you figured it all out. Great work!

Discussion closed.