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

conditional (dynamic) values for fields in CMS - backend


Go to End


5 Posts   2346 Views

Avatar
soolan

Community Member, 11 Posts

9 April 2013 at 9:01am

Edited: 09/04/2013 9:03am

Hi everyone,

I have a DataObject called Product and it has a few fields:

<?php
class Product extends DataObject {
//db fields
static $db = array(
'Name' => 'Varchar',
'MotorType' => 'Enum("-- select --,Induction,Single stage","-- select --")',
'Measurement'=> 'Enum("-- select --,KiloWatt,Watt","-- select --")'
// etc

);

My question is how can I (dynamically) set the value for Measurement field based on what I set for MotorType.

Assuming this is my getCMSFields() function:

//Fields for the DOM Popup
public function getCMSFields(){
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Specifications', new DropdownField('MotorType', 'Motor Type',singleton('Product')->dbObject('MotorType')->enumValues()));
// ToDo: dynamic values for next field
}

If I choose "Induction" for MotorType, I want the Measurement field pick up the "KiloWatt" value automatically
and
If I choose "Single stage" for MotoType, the Measurement field should be set with "Watt" value (again dynamically).

Any thoughts?

Your help is much appreciated.
Soolan.

Avatar
zenmonkey

Community Member, 545 Posts

11 April 2013 at 1:22pm

Easist solution is to use jQuery to set the field. If you don't want the end user to able to change it you could just hide the field and do set in an onBeforeWrite.

Avatar
soolan

Community Member, 11 Posts

11 April 2013 at 3:58pm

Thanks zenmonkey,
may I ask you to give me a quick example please?

Avatar
zenmonkey

Community Member, 545 Posts

11 April 2013 at 5:03pm

I forgot SS3 has moved to javascript dropdowns. Not sure if the use $("#FieldID").bind('change', function(){}); works. I woudl need to do a little more digging. Unless someone knows else knows off the op of their head how to bind events to SS3 dropdown changes

for the non user editable I would either remove the Measurment Field from the cms or only show it when ID == 0 so that it owuld show up for new items then add an onBeforeWrite

 public function onBeforeWrite() {
if ($this->MotorType == "Single stage") {
$this->Measurement = "Watt";
} else {
$this->Measurement = "KiloWatt";
}

parent::onBeforeWrite();
}

Avatar
soolan

Community Member, 11 Posts

12 April 2013 at 7:10am

much appreciated brother