Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set value of Repeater Field programmatically in Before Save Submission Event #2240

Open
nestordedios opened this issue Jan 15, 2025 · 1 comment

Comments

@nestordedios
Copy link

Describe the bug

I am encountering an issue when attempting to programmatically set the value of a repeater field during the EVENT_BEFORE_SAVE_SUBMISSION event. While I can successfully add rows to the repeater field, the internal fields within each row (text field in this case) are empty in the submitted form.

image

Steps to reproduce

  1. Create a repeater field in Formie with the handle repeater_field.
  2. Add a text input field inside the repeater with the handle text_field_in_repeater.
  3. Use the following PHP code in a custom module to populate the repeater field during the EVENT_BEFORE_SAVE_SUBMISSION event
Event::on(Submission::class, Submission::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
    $submission = $event->sender;
    $submission->setFieldValues([
        'repeater_field' => [
            ['text_field_in_repeater' => 'Test 1'],
            ['text_field_in_repeater' => 'Test 2'],
        ]
    ]);
}
  1. Submit the form

Form settings

  • Multi-page form: Yes
  • Submission Method: Page Reload
  • Client-side Validation: No
  • Custom Form Templates: No

Craft CMS version

4.13.10

Plugin version

2.1.38

Multi-site?

No

Additional context

No response

@engram-design
Copy link
Member

That'd be because Repeater fields are quite complex in Formie 2. Using this approach in Formie 3 would work.

In Formie 2, each block/row is an element, so you'll need to iterate through the value of the Repeater field, and modify each blocks' inner field value. It's identical to Matrix behaviour if you've had experience with that.

Event::on(Submission::class, Submission::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
    $repeaterField = $event->sender->getFieldByHandle('repeaterField');

    $row1 = new \verbb\formie\elements\NestedFieldRow();
    $row1->fieldId = $repeaterField->id;
    $row1->siteId = Craft::$app->getSites()->getCurrentSite()->id;
    $row1->setFieldValues(['textField' => 'Text 1']);
    $rows[] = $row1;

    $row2 = new \verbb\formie\elements\NestedFieldRow();
    $row2->fieldId = $repeaterField->id;
    $row2->siteId = Craft::$app->getSites()->getCurrentSite()->id;
    $row2->setFieldValues(['textField' => 'Text 2']);
    $rows[] = $row2;

    $event->sender->setFieldValues([
        'repeaterField' => $rows,
    ]);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants