3070 Posts in 869 Topics by 651 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1890 Views |
-
Setting dynamic default values in a DataObject form [SOLVED]

5 March 2010 at 2:26pm Last edited: 6 March 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
-
Re: Setting dynamic default values in a DataObject form [SOLVED]

6 March 2010 at 12:14am Last edited: 6 March 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
-
Re: Setting dynamic default values in a DataObject form [SOLVED]

6 March 2010 at 2:24am
Hi, I haven't tested it, but wouldn't populateDefaults() do the trick?
-
Re: Setting dynamic default values in a DataObject form [SOLVED]

6 March 2010 at 2:35am Last edited: 6 March 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
-
Re: Setting dynamic default values in a DataObject form [SOLVED]

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
| 1890 Views | ||
|
Page:
1
|
Go to Top |

