Jump to:

23550 Posts in 19336 Topics by 2890 members

General Questions

SilverStripe Forums » General Questions » Set CheckboxField to checked as default

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

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

Page: 1
Go to End
Author Topic: 3923 Views
  • bmc38119
    Avatar
    Community Member
    45 Posts

    Set CheckboxField to checked as default Link to this post

    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

  • bmc38119
    Avatar
    Community Member
    45 Posts

    Re: Set CheckboxField to checked as default Link to this post

    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.

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