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.

General Questions /

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

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

Set CheckboxField to checked as default


Go to End


3 Posts   7613 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.

Avatar
nickspiel

Community Member, 3 Posts

8 April 2015 at 2:06pm

Edited: 08/04/2015 2:07pm

This did the trick for me:

private static $db = array(
   "ShowInGoogleSiteMap" => "Boolean(1)"
):

Remove the field from the DB (if it exists) and run a dev build, the new default should now be applied.