Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
Hi all,

I am creating a form and I want the dropdown field to have dynamic options, where the options can be set/modified from the admin end instead of coding them into the form. Your help will be greatly appreciated.


PS: Thumbs up to KK and everyone in the house.
The Only Nigerian Good Son!
Please search forum, there are many (>10) posts about it.
@edydeyemi, Hello and welcome :)

@trendoman, you probably had the editable regions' 'dynamic' parameter (viewtopic.php?p=2483#p2483) in mind. I think @edydeyemi's is a different use-case where the options shown on the front-end form need to be made dynamic.

To be fair, even this use-case has been discussed (e.g. viewtopic.php?p=13629#p13629) but no harm in repeating ourselves a little :)

@edydeyemi,
Following is how we normally define a dropdown input in a form -
Code: Select all
<cms:input
    name="my_property_type"
    label="Property Type"
    desc="Select one from these"
    opt_values="Residential | Commercial | Rental"
    type="dropdown"
/>

In the code above, the 'opt_values' parameter is what sets the values shown in the dropdown.
Of course, those are hard-coded but then consider the following variation of the same code -

Code: Select all
<cms:set my_options = "Residential | Commercial | Rental" />

<cms:input
    name="my_property_type"
    label="Property Type"
    desc="Select one from these"
    opt_values="<cms:show my_options />"
    type="dropdown"
/>

In the amended code above, we are now setting the 'opt_values' parameter using a variable named 'my_options'.
So, if the value in that variable changes, so will the options shown by the dropdown. Right?

As you can see, our dropdown is no longer static but takes values from elsewhere.

With that concept clear, we can now make it easy for your clients to change the value within 'my_options' variable (which in turn will change the dropdown).

To do that, define a type 'text' or 'textarea' editable region in your template naming it 'my_options'. Ask your client to enter within that region values separated by '|' characters (e.g. what we used above 'Residential | Commercial | Rental').

As you know, when the template runs, the value inputted within the region is automatically made available as normal variable having the same name as that region.

So now we can remove the '<cms:set my_options =' we used above. Just the following code will show the dropdown dynamically as you asked for -
Code: Select all
<cms:input
    name="my_property_type"
    label="Property Type"
    desc="Select one from these"
    opt_values="<cms:show my_options />"
    type="dropdown"
/>

To reiterate, the idea is to input the option values in an editable region and then use its values within <cms:input>.

Hope it helps.
Wow!!! Wonder why I never thought of it this way.
Thanks KK, U just made my day.
The Only Nigerian Good Son!
4 posts Page 1 of 1
cron