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.

Data Model Questions

Setting dynamic default values in a DataObject form [SOLVED]


Reply

5 Posts   2686 Views

Avatar
Judge

5 March 2010 at 2:26pm (Last edited: 6 March 2010 3:04am), Community Member, 79 Posts

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

6 March 2010 at 12:14am (Last edited: 6 March 2010 1:10am), Community Member, 79 Posts

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

6 March 2010 at 2:24am Forum Moderator, 1091 Posts

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

Avatar
Judge

6 March 2010 at 2:35am (Last edited: 6 March 2010 2:35am), Community Member, 79 Posts

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

6 March 2010 at 2:46am Community Member, 79 Posts

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