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, simon_w

Default value for CalendarDateField


Reply


2 Posts   1357 Views

Avatar
freefall

Community Member, 5 Posts

16 November 2009 at 6:45pm

Hey Everyone,

I have the following code for the CMS Fields:

function getCMSFields() {
      
      // Get the parent fields
      $fields = parent::getCMSFields();
      
      // Others removed
      $fields->addFieldToTab("Root.Content.ArticleDetails", new CalendarDateField("Date", "Article date", date("d/m/Y")));
      // Others removed
      
      // return the fields
      return $fields;
      
   }

However, this is not setting the default value when I create a new item.

I have followed this thread: http://silverstripe.org/data-model-questions/show/266116?start=0 however I get an error when adding the $default variable to my class, just underneath the static $db = array() block.

Could someone please advise how to achieve this?

Cheers,
Justin

Avatar
dalesaurus

Community Member, 283 Posts

17 November 2009 at 5:47am

The ways outlined in the other thread should work to add to your $default static. If you are getting errors you must have something syntactically wrong.

Another Option to default the date would be to make use of the onBeforeWrite() function to set your $this->Date = date('Y-m-d');
Note the use of ISO-8601 date format.