Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

disabling/enabling fields based on dropdown selection


Go to End


7 Posts   5124 Views

Avatar
Carbon Crayon

Community Member, 598 Posts

1 January 2009 at 12:12am

Hi guys

I am trying to grey out some fields in the CMS based on the selection of a dropdown above. So I have a drop down with home/away as options and then I have an away club address underneath which I want to be greyed out and uneditable if home is selected.

Any ideas?

cheers

aram

Avatar
UncleCheese

Forum Moderator, 4102 Posts

1 January 2009 at 4:48am

Hi, aram,

You need to use the Behaviour class to add a javascript behavior to your select box. See the file I posted here http://www.silverstripe.org/customising-the-cms/show/251409. I did this sort of thing in the VerticalTabSet class. The only difference is yours will use an onchange event, but for the most part, you should be able to copy a lot of that code.

Let me know if you have any questions.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

1 January 2009 at 4:49am

Also, if you're interested in using radio buttons instead, the SelectionGroup field is really easy to use. (the behaviour you see in a FileIFrame field, for instance)...

Avatar
Carbon Crayon

Community Member, 598 Posts

1 January 2009 at 8:04am

Edited: 01/01/2009 8:05am

thanks for the help :)

I tried to get the Java stuff working but failed miserably, which isn't so surprising as i have yet to properly learn any javascript!

I think SelectionGroup souds like a great solution for me right now so I will leave the dropdown untill I have time to learn a bit of js.

I cant seem to find any documentation on the SelectionGroup and the API doc isnt incredibly clear on implementation, any chance you could post an example?

Once again, thanks :)

Oh and Happy new year! I think 2009 will be a good year for SS

Avatar
UncleCheese

Forum Moderator, 4102 Posts

1 January 2009 at 8:57am

I use it in ImageGalleryAlbum.php, if you have that handy.

$f->push(new SelectionGroup("Source", array(
"new//Create a new folder" => new HiddenField(""),
"existing//Use an existing folder" => new FieldGroup("",
new DropdownField("FolderID","",DataObject::get("Folder",null,"Title ASC")->toDropdownMap('ID','Filename'))
)
))
);

where the text before the // is the value of the radio button and after it is the label. Notice i use an empty hiddenfield, since in this case, clicking "use an existing folder" exposes nothing.

Also, if you plan to use this in a popup, you'll need to use the PopupSelectionGroup that i created for the image gallery.

Avatar
Carbon Crayon

Community Member, 598 Posts

2 January 2009 at 1:13am

mate your a legend that works perfectly!

I love adding details like that to the interface it really seems to add an extra level usability.

anyway thanks so much for your help :)

Avatar
Nexus Rex

Community Member, 8 Posts

3 May 2009 at 3:40am

aram - Do you mind posting the full code for your form that you used to disable/enable fields based upon another fields selection value. I need to get this done, but would appreciate a more simple step-by-step explanation.

A link to see the live example would be great as well.

Thank you,

Travis Cable