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.

Customising the CMS

conditional (dynamic) values for fields in CMS - backend


5 Posts   522 Views


9 April 2013 at 9:01am (Last edited: 9 April 2013 9:03am), Community Member, 11 Posts

Hi everyone,

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

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
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.


11 April 2013 at 1:22pm Community Member, 528 Posts

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.


11 April 2013 at 3:58pm Community Member, 11 Posts

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


11 April 2013 at 5:03pm Community Member, 528 Posts

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";



12 April 2013 at 7:10am Community Member, 11 Posts

much appreciated brother