Jump to:

5539 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » conditional (dynamic) values for fields in CMS - backend

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

Page: 1
Go to End
Author Topic: 497 Views
  • soolan
    Avatar
    Community Member
    11 Posts

    conditional (dynamic) values for fields in CMS - backend Link to this post

    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.

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: conditional (dynamic) values for fields in CMS - backend Link to this post

    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.

  • soolan
    Avatar
    Community Member
    11 Posts

    Re: conditional (dynamic) values for fields in CMS - backend Link to this post

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

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: conditional (dynamic) values for fields in CMS - backend Link to this post

    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();
    }

  • soolan
    Avatar
    Community Member
    11 Posts

    Re: conditional (dynamic) values for fields in CMS - backend Link to this post

    much appreciated brother

    497 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.