Skip Navigation

[Resolved] Dynamic CRED Generic Field Options does not work.

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 8 replies, has 2 voices.

Last updated by Amr 4 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1796263

Amr

In a CRED form, I am trying to dynamically generate the list of options that should go into a "Select" generic field like this:

[credform]
[cred_field field='form_messages' class='alert alert-warning']
<div class="form-group">Select a new author:</div>

[cred_generic_field field="my_author_select" type="select" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":"",
"options":[list-of-authors-options]
}
[/cred_generic_field]

[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]

The shortcode [list-of-authors-options] is in my function.php and is as follows:

// Add Shortcode to Get a List of authors
add_shortcode("list-of-authors-options", "list_of_authors_options");
function list_of_authors_options() {
$out = '';
$users_subscriber = get_users(array( 'role' => 'Subscriber' ));
$users_superuser = get_users(array( 'role' => 'Superuser' ));
$users = array_merge($users_subscriber,$users_superuser);
$count = count($users);
$i=1;
foreach ($users as $user) {
if ($i==$count) {
$out .= '{"value";"' . $user->ID . '","label":"' . $user->display_name . '"}';
}else{
$out .= '{"value";"' . $user->ID . '","label":"' . $user->display_name . '"},';
}
$i =$i + 1;
}
return $out;
}

If I insert this shortcode on a page, I can clearly see the output as follows which is the proper required format. However, the CRED form shows no options whatsoever.

{"value";"1","label":"name1"},{"value";"2","label":"name2"},{"value";"3","label":"name3"},{"value";"4","label":"name4"}

I also tried to implement an alternate solution by using a View named "authors-dropdown-options" as a shortcode argument for the "options" parameter like this:

"options":[[wpv-view name="authors-dropdown-options"]]

Here is the Views code:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-item index=other]
{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"},
[wpv-item index=last]
{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-layout-end]

Again inserting this view on a page does generate the correct output, but it does not work in the CRED form.

Please help. Thank you.

#1796475

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

We have another report of this where the user is generating the dynamic options with a View, and downgrading Views fixes the problem.

However, in your case if you are registering a custom shortcode to output the options and they are not working, that doesn't sound like the problem is in Views, it sounds like it is in Forms.

Could you try downgrading both Forms (to 2.5.8) and Views (to 3.2.2), which you can get from the changelogs on the downloads page (Views in the legacy tab): toolset.com/account/downloads

First verify that your shortcode then works, and that your View works.

Then try alternately updating Forms or Views and see which provokes the problem?

#1797181

Amr

Hello Nigel,

I first downgraded "Forms" to version 2.5.8 at tested and It did not work. I then downgraded "Views" to Version 3.2.2 and tested and I still could not get it to work. I then switch from using the Custom shortcode in the function.php to using the equivalent Views version and It still did not work.

I should mention that this functionality of dynamically generating the list of options worked for the longest time until we updated "Forms" and "Views" as well as WordPress. I am not sure which of the upgrades triggered the functionality to stop working.

Please help us resolve this issue.

Thanks

#1797835

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The developers identified a problem with Views that explains why it has broken when using a View to provide the dynamic options, but I'm not sure that helps explain why it is not working with your custom shortcode.

The fix will be included in the next release of Views, but a patch is available in the erratum here: https://toolset.com/errata/using-a-view-with-json-output-to-populate-generic-select-field-in-a-form-not-working/

Could you try applying that patch and then re-test, and let me know what does and does not work?

If you are still having problems I'll get more details to share with the developers.

#1798333

Amr

Hi Nigel,

So I restored the latest version of View v 3.3.1 then I applied the patch (replaced the file "internals.php"). I tried both the Views shortcode as well as the custom shortcode to dynamically get the select field option. Unfortunately, none of them worked.

Thanks

#1799825

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, so there seems to be something different going on with your site than the other reports.

Could I get a copy of the site to do some testing locally?

You can share packages via dropbox or similar here.

hidden link

#1808759

Amr

Hello Nigel,

Thank you for your reply. It is difficult to provide you with a copy of the site at this time. To solve my problem, I hade to change course and take a different approach.

Instead of relying on a generic field and have to generate the JSON select option like this:
{"value":"1","label":"name1"},{"value":"2","label":"name2"},{"value":"3","label":"name3"},{"value""4","label":"name4"}

using either a view or a custom shortcode function (neither worked), I am now using a custom field and a filter in the function.php like this. This solution works. However, it is unfortunate that the generic field solution stoped working. I hope a solution to this problem would be found soon.

Thank you.

---------------------
add_filter( 'wpt_field_options', 'selectfieldoptions', 10, 3);
function selectfieldoptions( $options, $title, $type ){

switch( $title ){

case 'Author':

$options = array();
$users_subscriber = get_users(array( 'role' => 'Subscriber' ));
$users_superuser = get_users(array( 'role' => 'Superuser' ));
$authors = array_merge($users_subscriber,$users_superuser);

foreach ($authors as $user) {
$options[] = array(
'#value' => $user->ID,
'#title' => $user->user_login,
);
}

break;

}
return $options;
}

#1808763

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I'm glad you have a workaround.

After your feedback about it not working I recreated a test site with the current plugin versions and applied the patch from the erratum, and I was able to use the output of a View to generate the JSON for the options to populate a generic select field in a form, it worked as expected.

So from our perspective we don't have anything to fix, unless we get steps to reproduce or a copy of a site that demonstrates the problem.

I suggest we close here unless you are able to replicate on a simplified version of your site that you could share.

#1808927

Amr

My issue is resolved now. Thank you!