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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Set CheckboxField to checked as default


Go to End
Reply


2 Posts   4281 Views

Avatar
bmc38119

Community Member, 45 Posts

17 September 2009 at 10:42am

I am trying to add a Checkbox field to the CMS (Page Class) and having no luck in setting it to be checked by default. My code is below.

class Page extends SiteTree {
	
	public static $db = array(
	'TemplateStyle' => 'Enum("Blue,Brown,Green,Orange,Home")',
	'ShowInGoogleSiteMap' => 'Boolean'
	);
	
	public static $has_one = array(
	);
	static $defaults = array(
		"ShowInGoogleSiteMap" => 1
	);
	
	/******************************************
	* Add fields to CMS
	*******************************************/
	
	function getCMSFields() {
		$fields = parent::getCMSFields();

		$fields->addFieldToTab('Root.Behaviour', new CheckboxField('ShowInGoogleSiteMap','Show in Google SiteMap?'),'ProvideComments'); 
		
	return $fields;
	}

Attempted Solutions:

1. I have tried adding a default value as a parameter like the following. But this does not work - nor does it look like this is a parameter in the API doc.

		$fields->addFieldToTab('Root.Behaviour', new CheckboxField('ShowInGoogleSiteMap','Show in Google SiteMap?'),'ProvideComments'); 

2. I have also tried adding this to the Page Class as well. But this does not work either.

		public function populateDefaults(){
		parent::populateDefaults();
		$this->ShowInGoogleSiteMap = 1;
		}

Anyone know how to make this work?

Thanks

Avatar
bmc38119

Community Member, 45 Posts

17 September 2009 at 11:24am

Just solved.

I was testing on an already created page and this page already had a default value of '0' in the database. Once I tested on a newly created page, it looks like the static $defaults array takes care of it.