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

[solved] DateFormat in HasOneComplexTableField


Go to End
Reply

11 Posts   1618 Views

Avatar
quanto

17 August 2011 at 10:21pm Community Member, 81 Posts

I'm trying to format the date into 'HasOneComplexTableField' and 'FieldSet'. I only try to put the date in the right format in the CMS.

For the fieldset i've got this code:

function getCMSFields_forPopup() {
$fields = new FieldSet();
$fields->push( new DateField( 'Date', 'Datum') ); //<-- This date has to be formatted into dd-MM-YYYY
$fields->push( new TextareaField( 'ShortDesc', 'Korte beschrijving' ) );
$fields->push( new TextareaField( 'LongDesc', 'Lange Beschrijving' ) );

return $fields;
}

For the 'HasOneComplexTableField' i've got this code:

$tablefield = new HasOneComplexTableField(
$this,
'MyCalendar',
'Calendar',
array(
'Date' => 'Datum',//<-- This date has to be formatted into dd-MM-YYYY
'ShortDesc' => 'Korte beschrijving',
'LongDesc' => 'Lange beschrijving'
),
'getCMSFields_forPopup'
);

In my __config.php i did setup the i18n settings:

// Set the site locale
i18n::enable();
i18n::set_locale('nl_NL');
i18n::set_date_format('dd-MM-YYYY');

I tried several things, like $fields->setConfig, but it returns the method 'setconfig' does not exist on 'FieldSet'. Is there any regular way to format these fields right?

Avatar
swaiba

17 August 2011 at 10:31pm Forum Moderator, 1796 Posts

this is one thing I keep in a Utils class...

class Utils {
   public static function MakeCalendarPopup($strFieldName,$strLabel) {
      $df = new DateField($strFieldName,$strLabel);
      $df->setConfig('showcalendar', true);
      $df->setLocale('nl_NL');
      $df->setConfig('dateformat', 'dd-MM-YYYY');
      return $df;
   }
}

then in cmsfileds....

      $f->replaceField('Date', Utils::MakeCalendarPopup('Date', 'Date:'));

Avatar
quanto

17 August 2011 at 10:52pm Community Member, 81 Posts

Thanks for the calendarpopup! The Fieldset works fine by now. The HasOneComplexTableField doesn't change to the right date. See attached image. Any idea?

Attached Files
Avatar
swaiba

17 August 2011 at 10:58pm Forum Moderator, 1796 Posts

yep, using casting of summary fields like this...

   public static $summary_fields = array (
...
      'DateText'         => 'Date',
...
   );

   static $casting = array(
      "DateText" => "Text",
   );

   public function DateText() {
      return <<add logic to format date top string here>>;
   }

Avatar
martimiz

17 August 2011 at 11:03pm Forum Moderator, 1091 Posts

Also: using the ComplexTableField::setFieldFormatting() function will format the fields as they appear in the list (not the popup). Something like:

function formattedDate() {
   //return formatted date
}

$tablefield->setFieldFormatting(array(
   'Date' => $this->formattedDate()
));

Avatar
quanto

18 August 2011 at 12:21am Community Member, 81 Posts

Sorry, stupid question: but how do i push the date into the function? $this->Date doesn't work.

Avatar
martimiz

18 August 2011 at 1:17am (Last edited: 18 August 2011 1:17am), Forum Moderator, 1091 Posts

Not a stupid question at all :-) It has been a while, so I didn't quite remember how it went :-(

It's the Calender object that knows its own date, so this should go into your Calendar class:

public function getFormattedDate() {
   return date('d-m-Y', strtotime($this->Date)); // replace with however you want to do it
}

Next for your ComplexTableField:

$tablefield->setFieldFormatting(array(
   'Date' => '$FormattedDate')
));

The code '$FormattedDate' will be eval-ed for the Calender object, hence the quotes... So now I remember :-)

Avatar
quanto

18 August 2011 at 2:34am Community Member, 81 Posts

Hmm. $this->Date still doesn't work :(. Is there an other option to get the date from the HasOneComplexTableField?

Go to Top