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

Select value in dropdownfield by default based on parent


Go to End


2 Posts   2012 Views

Avatar
klikhier

Community Member, 150 Posts

21 May 2009 at 2:53am

Edited: 21/05/2009 2:54am

What I want to do is to select a certain value in a dropdownfield by default based on the value of the parent (that has the exact same dropdownfield). Why can't I get this field to display the parent value by default in the CMS?

public static $db = array(
  'ResponsibleOffice' => 'Int'
};

(...)

/**
  * Overload so that the default value is same as parent. 
  * Based on same function in BlogEntry.php
  */
  public function populateDefaults() {
    parent::populateDefaults();
      $this->setField('ResponsibleOffice', $parent->ResponsibleOffice);
  }

(...)

  public function getCMSFields() {
    $fields = parent::getCMSFields();

    $options = array('Value1', 'Value2', 'Value3', 'Value4');
    $fields->addFieldToTab("Root.Content.Main", new DropdownField("ResponsibleOffice", "Responsible office", $options), 'Content');

    return $fields;
  }	

Avatar
klikhier

Community Member, 150 Posts

21 May 2009 at 10:14pm

Found populatedefaults recipe here. Following this example I now have:

	public function populateDefaults() {
		parent::populateDefaults();
		$parent_object = $this->getParent();
		$this->ResponsibleOffice = $parent_object->getField('ResponsibleOffice');
	}

Now, when adding this page as a child to another page, I get a 'Javascript parse error' in the CMS. What am I doing wrong?