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.

Data Model Questions /

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

Setting dynamic default values in a DataObject form [SOLVED]


Go to End


5 Posts   5645 Views

Avatar
Judge

Community Member, 79 Posts

5 March 2010 at 2:26pm

Edited: 06/03/2010 3:04am

Using the standard scaffolding, how would you do about setting default values for fields in a DataObject's form?

I have tried this, but it does not seem to work:

class Employee extends DataObject {
...

    function getCMSFields()
    {
        $fields = parent::getCMSFields();

        if (!$this->ID) {
            // New record: set dynamic defaults.
            $fields->setValues(array(
                'StartDate' => date('Y/m/d'),
                'EmployeeNumber' => $this->getNextEmployeeNumber(),
            ));
        }

        return $fields;
    }
}

I was expecting the setValues() method to set the values of the fields before being presented on the new item form, but they are not. That section of code *is* executed though.

Any idea what could be going wrong, or how to set default values for DataObject fields that are not simple static values?

-- Jason

Avatar
Judge

Community Member, 79 Posts

6 March 2010 at 12:14am

Edited: 06/03/2010 1:10am

I think the problem is that $fields does not directly contain the fields in the displayed form, and so setValues() cannot find the fields to update.

$fields contains the Root tab that contains the fields in the form. getFormFields() is not simply form fields - it also contains the nested tabs used in the screen to display the form.

I'm still tearing my hair out over what should be an extremely simple thing: how to set the value of a form item before the form is displayed. This what I'm trying at the moment, but the text form item resolutely does not change:

    function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $myField = $fields->dataFieldByName('myField');
        $myField->setValue('blah blah blah');

        echo $myField->value(); // This displays "blah blah blah"

        $fields->replaceField('myField', $myField);

        return $fields;
    }

What am I doing wrong? If I var_dump() $myField, it appears to be the correct field object with the updated value. But somehow it does not end up on the form.

-- Jason

Avatar
martimiz

Forum Moderator, 1391 Posts

6 March 2010 at 2:24am

Hi, I haven't tested it, but wouldn't populateDefaults() do the trick?

Avatar
Judge

Community Member, 79 Posts

6 March 2010 at 2:35am

Edited: 06/03/2010 2:35am

I'll give that a try. Thanks.

I just realised too that when replacing the primitive datatypes (e.g. text, numbers) with the form fields (e.g. email, calendar, textareas) you don't need to to move the value of the field you are replacing into the new new field you have created.

For example, this will turn a text field into an e-mail field:

static $db = array('Email' => 'Varchar(250), ...)

function getCMSfields()
{
...
// You can try setting a value here (parameter #3) but it will just be discarded.
$fields->ReplaceField('Email', new EmailField('Email', 'E-mail'));
...
}

The value is automatically copied across from the existing database record, or from the $defaults array of the DataObject. That means no matter what values you set in getCMSFields(), they will get overridden further down the line before the form is displayed.

-- Jason

Avatar
Judge

Community Member, 79 Posts

6 March 2010 at 2:46am

Yes, that was it. The documentation is here:

http://doc.silverstripe.org/doku.php?id=recipes:populatedefaults

I searched, and searched, and searched again, but did not find it.

This piece of documentation needs to be here, where the other scaffolding information is, including the method of adding static defaults:

http://doc.silverstripe.org/doku.php?id=dataobject

At least a little hint to point at the API documentation (http://doc.silverstripe.org/doku.php?id=dataobject) would be good on that page.

-- Jason