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 a default date on page load


Go to End


2 Posts   1217 Views

Avatar
Bagzli

Community Member, 71 Posts

23 March 2015 at 5:34am

Edited: 23/03/2015 5:50am

How do we set today's date to be populated by default on a Date field when page loads?

Here is what I Tried:

    static $defaults = array(
        'GameDate' => 'date(\'Y-m-d\')'
    );

    public function GameResultForm() {

        $gameDate = new DateField('GameDate');
        $gameDate->setConfig('showcalendar', true);
        $gameDate->setConfig('dateformat', 'dd-MM-YYYY');
        $gameDate->setConfig('max', date('Y-m-d'));
        $fields = new FieldList (
            $gameDate
        );

        $actions = new FieldList(
            new FormAction('saveNewGame', 'Add New Game')
        );

        $form = new Form($this, 'GameResultForm', $fields, $actions,);
        $form->setFormMethod('POST', true);

        $data = Session::get("FormData.{$form->getName()}.data");
        return $data ? $form->loadDataFrom($data) : $form;

I also tried

        $gameDate->setConfig('setValue', date('Y-m-d'));

Avatar
Pyromanik

Community Member, 419 Posts

25 March 2015 at 10:36am

Edited: 25/03/2015 10:46am

At first I thought you meant

private static $defaults = ['Date'=>'now'];

But reading it seems you are confused about DateField's configuration (min,max,1 field or 3[d,m,y], to javascript or not), and setting just about any FormField's value.

$datefield->setValue(date('Y-m-d'));

http://api.silverstripe.org/3.1/class-DateField.html#_setValue