Skip Navigation

[Resolved] Dynamic Select Fields in Post Fields or Forms

This thread is resolved. Here is a description of the problem and solution.

Problem:
Can we control the options of a Select field depending on what was chosen in another, preceeding Field, in Toolset Types or Forms?

Solution:
There is no solution to dynamic inputs fields listening to other fields values, neither in forms nor types.
This would require a new Feature - which can be suggested here:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

This support ticket is created 5 years, 10 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 9 replies, has 2 voices.

Last updated by Beda 5 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#1203300

Ed

I want to create a form that has select fields to choose relationships to State, County, and City CPTs.

When the user chooses a State, the second select field will only show Counties in that State. The same would go for the City select field, based on the selected County.

Is this possible?

#1203604

This is not possible in Toolset.
Each Field input is static and listens to a preset set of data.
You can manipulate this set by for example passing only posts by current user, or ordering by ID, etc, but you cannot make it listen to another Field in the same form.

This would require a new Feature - which can be suggested here:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

You cannot use "conditional" statements in the value attribute of the parent post picker because you would run into issues with overusage of quotes (as ShortCodes feature quotes and if quotes are nested, WordPress "breaks" the ShortCodes)

#1203653

Ed

Thanks, Beda. I kinda thought that was the case.

So...in order for a user to post content with the correct relationship(s), would I have to send them to the correct "location" (CPT), before letting them create a post?

I'm wondering, now, if I should ditch the whole "location" CPT system and use taxonomies for states, counties, and cities, instead.

Maybe I'm still having trouble wrapping my head around this.

#1203676

Submitting this as a Feature Suggestion, because there is nothing else that can solve this requirement, would be the right thing to do.
As mentioned, the field is static, it does not listen to dynamic inputs, and that must be changed so to allow what you need.

To solve the issue about users eventually connecting the wrong posts, I assume you have 2 "parent" post types to the currently created(edited) post type and one is the country the other a state.
So one solution could be to use a View, that lists all States of a Country for example, and in the View display the Form.
The View would be set to display only States that are related to the Country where the View is displayed on, this way, that front end only displays Forms to edit States of the current country.

#1203708

Ed

"County", not "country", but yes...that's exactly what I'm trying to avoid. And it goes even further, when a user is posting a "Listing", that belongs to City, County, and State CPTs.

But, even with that solution, they could still choose a city that doesn't belong in the selected County, even if they're "in" the correct State.

I don't think there's a solution for this, other than the fantasy solution of the three select fields working off each other.

I hope my explanation makes sense.

#1203719

Sorry, right, county.

Yes, there is no solution for this, unless you would set the form to a post and make the form to add child posts (which as a parent have the current post)
That requires a View, that displays the Child Posts of the Current post, so you can insert the Form in it, and let it create child posts to the one displayed.

So you would probably have a Grand Parent Post (State?) then query Parents (County?) in a View, and put that View in the Grand Parent.
The view has a filter to only return the related posts.
In the View, you can create a link to the Form so to let it submit only posts that will be a child to the current post.
That will avoid a post being submitted to the wrong parent because it won't be possible to choose another parent altogether.

But that also requires leading the user to the right post first, it cannot be done within the same page/form.

Creating Forms to add Child Posts is elaborated here.
https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/
Note also this is only valid for one-to-many or one-to-one relations.

Would that help?

#1203744

Ed

Well...yes...that would help, but this is something I've kinda been doing, for a while, on other sites. It's not the best solution (for users, anyway).

I guess I'm dreaming too big. I'll see if I can post this as a Feature Suggestion.

Thanks for your help!

#1203751

That is the only solution I can think of.

Unless you want to venture in complex Custom Code validation that upon some conditions will stop the user submitting the form if wrong data is chosen.
That could be done within a cred_save_data() https://toolset.com/documentation/programmer-reference/cred-api/.

In there you'd need to get the value is chosen in the Selectors (that you could get from the $_POST or with the Toolset Associations API toolset_get_related_posts https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/ and then compare those values with a list of "can or cannot" connect.
This requires you to hardcode each chain of grandparent-parent-child that you want to allow, so you can later check "if the connection is not as allowed" - abort.

That is not what I recommend as it is simply not effective, it is less effective than having the form on all posts in form of an "Add Childs" Post Form.

Unfortunately, we cannot solve this elsehow just yet.
Custom Code is subject to specific Support rules, and I cannot provide a working version of such code here.

I can, however, help with a review or give tips where you get stuck with.

#1203755

Ed

Uhh...yeah, I'll pass on that one. 🙂

I appreciate your help.

#1203771

Then I close here.

Please let us know if you need further help with this