Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In SilverStripe usually the 3rd argument to a FormField is the value, so for example a TextField has the following arguments:</p> <pre><code>new TextField($name = 'myField', $title = 'Please write something in my Field', $value = "yay"); </code></pre> <p>but this would not work in the CMS (at least in SilverStripe 2 if you are using a Page, not sure on DataObject) because SilverStripe overwrites all values when it tries to populate the Form with the values of the current object</p> <p>so you have several alternatives, the 2 easiest alternatives are:</p> <pre><code>class MyContentObject extends DataObject { public static $db = array( 'Text' =&gt; 'HTMLText', ); public static $has_one = array( 'Member' =&gt; 'Member', ) public function getCMSFields() { $fields = new FieldSet(); $fields-&gt;push(new Textarea('Text', 'Text')); if (!$this-&gt;MemberID) $this-&gt;MemberID = Member::currentUserID(); $fields-&gt;push(new HiddenField('MemberID')); return $fields; } } </code></pre> <p>And 2nd option, which is way better in this case, you don't even need a hidden field, you can just set the MemberID right before the record gets written to database by using onBeforeWrite:</p> <pre><code>class MyContentObject extends DataObject { public static $db = array( 'Text' =&gt; 'HTMLText', ); public static $has_one = array( 'Member' =&gt; 'Member', ) public function getCMSFields() { $fields = new FieldSet(); $fields-&gt;push(new Textarea('Text', 'Text')); return $fields; } public function onBeforeWrite() { // this method will be called every time the object gets saved parent::onBeforeWrite(); if (!$this-&gt;MemberID) $this-&gt;MemberID = Member::currentUserID(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload