Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Setting dynamic default values in a DataObject form [SOLVED]

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

Page: 1
Go to End
Author Topic: 2418 Views
  • Judge
    Avatar
    Community Member
    79 Posts

    Setting dynamic default values in a DataObject form [SOLVED] Link to this post

    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

  • Judge
    Avatar
    Community Member
    79 Posts

    Re: Setting dynamic default values in a DataObject form [SOLVED] Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Setting dynamic default values in a DataObject form [SOLVED] Link to this post

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

  • Judge
    Avatar
    Community Member
    79 Posts

    Re: Setting dynamic default values in a DataObject form [SOLVED] Link to this post

    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

  • Judge
    Avatar
    Community Member
    79 Posts

    Re: Setting dynamic default values in a DataObject form [SOLVED] Link to this post

    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

    2418 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.