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

[solved] DateFormat in HasOneComplexTableField


Go to End


11 Posts   2526 Views

Avatar
quanto

Community Member, 91 Posts

17 August 2011 at 10:21pm

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

Forum Moderator, 1899 Posts

17 August 2011 at 10:31pm

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

Community Member, 91 Posts

17 August 2011 at 10:52pm

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

Forum Moderator, 1899 Posts

17 August 2011 at 10:58pm

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

Forum Moderator, 1391 Posts

17 August 2011 at 11:03pm

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

Community Member, 91 Posts

18 August 2011 at 12:21am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

18 August 2011 at 1:17am

Edited: 18/08/2011 1:17am

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

Community Member, 91 Posts

18 August 2011 at 2:34am

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

Go to Top