Search code examples
phpwordpresspluginsformidable

How can I createa a new repeater field in WordPress Formidable Forms without it getting the previous entry values as default?


I want to populate a Wordpress Formidable repeater field with several values. I've created a custom Wordpress plugin with custom code and used https://formidableforms.com/knowledgebase/frm_setup_new_fields_vars/#kb-auto-populate-multiple-rows-in-a-repeater as a reference.

My solution works, however with one hick-up. I can create a new entry without any issues and it shows up as an entry. However, the next new entry is fed the old entries values as defaults. Note that i can still edit that new form and save a new entry.

Somewhere in this Formidable plugin the old entry is assumed as the one to use for default values. What should i change in order to always create a fresh entry (so without unwanted default values in the repeater row)?

Is there perhaps something wrong with the assumption 'form_select' is the correct child form id? Or is there something missing like an 'entry_id' to state this is a new entry?

class formidableRepeater

{

public static function populateRepeaterFieldsWithNames($result, $field)
{

    //simplified a bit and removed non-relevant code
    $names = ['jack', 'jill', 'joe'];
    $result['value'] = self::getValueRowsForResult(FormidableFieldKey::PREFILL_REPEATER_NAME, $names, $result['form_select']);

    return result;
}

private static function getValueRowsForResult($fieldKey, $values, $form_select): array
{
    $result = [];

    //simplified a bit and removed non-relevant code
    $valueRows = static::getValueRows($fieldId, $values);

    $result = $valueRows;
    $result['form'] = $form_select;
    $result['row_ids'] = array_keys($valueRows);

    return result;
}

private static function getValueRows($fieldId, $values): array
{
    $result = [];
    $i = 0;

    $finalValues = is_array($values) ? $values : [$values];
    foreach ($finalValues as $value) {
        $result[$i] = static::getValueRow($i, $fieldId, $value);
        $i++;
    }

    return $result;
}

private static function getValueRow(int $rowNumber, $fieldId, $value): array
{
    return [
        $rowNumber => '',
        $fieldId => $value
    ];
}

}


Solution

  • Turns out, this only works for text fields. Formidable states (in a support question):

    Just to clarify, checkboxes and radio buttons works differently. That function is to be used for setting default values in text fields and not radio buttons/dropdown fields/checkbox fields.

    Would have saved me a few hours if this information was written at the link.

    I found a 'kind of' solution here. If you want this to work, make sure to create a first entry without any values voor the radio / checkbox / dropdown. The next time you create an entry, the default are still loaded, but they are empty and do not interfer with your form!