Dynamic Field limit entries by current user option not working

By: Yaroslav Kurilo | Asked: 10/06/2024
ForumsCategory: How-toDynamic Field limit entries by current user option not working
Yaroslav Kurilo asked 2 months ago
For some reason when i enable " Limit options to those created by the current user" for the dynamic field, the Form B no longer shown any options for the user to select from Form A.  I would like to limit the dynamic field in Form B to show entries made by the same user via Form A.
2 Answers
Victor Font answered 2 months ago
The obvious question is did the user of Form A ever create and entries in Form B to show in Form A? The second question is who is logged into the system, the user that created the entries in both Form A and Form B, or are they different users?
Yaroslav Kurilo replied 2 months ago

Yes, the user did create entries via Form A, but when I enable "Limit options to those created by the current user" for the dynamic field, Form B no longer shows any options for the user to select from Form A. The user is Admin. Did try with other users as well. The form is on a website under development and during testing, we realized that other users could see the entires/fields of different users. Also, i should mention that the form is on a private page, only logged-in users can see it. Thank you.

Victor Font replied 2 months ago

Who created the entries in Form B and how are they being created? You only answered about Form A. The entries in Form A and Form B must be created by the same user, even if the person is an admin.

The best way to confirm the root cause of your problem is to examine the data in the database. Use PHP admin to examine the entries in wp_frm_items for form Form A and the entries for Form B that are to appear in the lookup and make certain the user_ids in both datasets are the same.

Yaroslav Kurilo replied 2 months ago

Form A - collects data about the tenant, Form B - collects photos/docs/etc. Entries in Form B are created with a dynamic field that pulls name from Form A plus upload field etc.. If i don't choose the option to limit by a current user it works, but then the dynamic field shows all Form A entries and is viewed by all other users (they see clients that they didn't create) In PHP admin, user_id is correct. Dynamic field is a required option so I've disabled it to check in db the user_id and it is the same. That's why its' strange that it does not work.

Victor Font replied 2 months ago

I suggest you open a ticket with Strategy 11 support through your Formidable account. Also, make certain you're on V. 6.15.1. There was a bug in 6.15 that impacted lookup fields.

Yaroslav Kurilo answered 2 months ago

thank you i will do that. For now using the following workaround. 

add_filter('frm_setup_new_fields_vars', 'limit_dynamic_field_to_current_user', 20, 2);
function limit_dynamic_field_to_current_user($field, $args) {
    if ($field['type'] == 'data' && $field['id'] == YOUR_DYNAMIC_FIELD_ID) { // Replace with your dynamic field ID
        // Get the current user ID
        $current_user_id = get_current_user_id();

        // Check if the user is logged in
        if ($current_user_id) {
            global $wpdb;

            // Get the entries submitted by the current user
            $entries = $wpdb->get_results($wpdb->prepare(
                "SELECT id FROM {$wpdb->prefix}frm_items WHERE user_id = %d AND form_id = %d",
                $current_user_id,
                YOUR_FORM_ID // Replace with the form ID that you are pulling entries from
            ));

            // If there are entries, filter the dynamic field options
            if (!empty($entries)) {
                $entry_ids = wp_list_pluck($entries, 'id');
                $field['options'] = array_intersect_key($field['options'], array_flip($entry_ids));
            } else {
                // No entries found for the current user, make sure the field is empty
                $field['options'] = array();
            }
        }
    }

    return $field;
} 

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right