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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Setting a form field value based on a database entry


Reply


4 Posts   1288 Views

Avatar
evilish

Community Member, 9 Posts

1 February 2011 at 4:13am

Hey everyone,

Hope everyone's doing great.

In the process of rapping up a project and I'am trying to overcome a little issue.

I need to add a few additional fields to the Security/Members section. I have gone as far as adding a test SID field with the code pasted below and now I'am trying to figure out how to populate the SID form field. Once an entry has been made.

So basically, if I enter in 1234 as the SID for Tom Brown, hit save and then click back on Tom Brown at a later date. The SID form field will populate with Tom's SID db entry (1234).

I have tried to look through the documentation, tried a bunch of random Google entries, tried looking for an entry on addFieldToTab and I haven't had much luck.

I found the code to extend the Security/Members in another thread (credit Zauberfisch) and it consists off (with a few modifications):

<?php
class SecurityMembersExtension extends DataObjectDecorator {

   /**
   * Define extra database fields
   *
   * Return an map where the keys are db, has_one, etc, and the values are
   * additional fields/relations to be defined
   */
   function extraMemberDetails() {

      $fields = array(
         'db' => array(
            'SID'      => 'Text'
         ),
            'has_one'   => array(
         ),
            'defaults'   => array(
               'SID'   => 'No SID entered'
         )
      );

      return $fields;
   }

   function updateCMSFields(FieldSet &$fields) {

      $fields->addFieldToTab('Root.Details', new TextField("SID", "SID"));

      return $fields;

   }

}

As always, any help is much appreciated.

Cheers,
- Evilish

Avatar
evilish

Community Member, 9 Posts

2 February 2011 at 5:38am

Hey guys, anyone have any ideas? Mabye seens something that might steer me in the right direction?

Cheers,
- Evilish

Avatar
Willr

Forum Moderator, 5513 Posts

2 February 2011 at 9:09pm

It should work fine out of the box. If SID is set in the database then loading that page should load it automatically so it sounds strange that it isn't so double check the SID value is set in the database.

You can set the value of a TextField by passing the 3rd argument.

$fields->addFieldToTab('Root.Details', new TextField("SID", "SID", $this->owner->SID));

If that still doesn't work, debug $this->owner->SID I think you'll find it is blank, or getting override somewhere.

Avatar
evilish

Community Member, 9 Posts

4 February 2011 at 4:10am

Cheers for the reply mate.

I don't know how many forum posts and documentation pages I went through but I found a reference that actually helped me get the form field value to show.

http://doc.silverstripe.org/sapphire/en/reference/dataobjectdecorator

I modified the code I posted earlier to something similar to whats described on the dataobjectdecorator page and I ended up with:

<?php
class SecurityMembersExtension extends DataObjectDecorator {

   /**
   * Define extra database fields
   *
   * Return an map where the keys are db, has_one, etc, and the values are
   * additional fields/relations to be defined
   */
   function extraStatics() {

      $fields = array(
         'db' => array(
            'SID'            => 'Text'
         )
      );

      return $fields;
   }

   function getCMSFields() {
      
      $this->extend('updateCMSFields', $fields);

   }

   function updateCMSFields(FieldSet $fields) {

      $fields->addFieldToTab('Root.Details', new TextField("SID", "Student ID"));

   }

}

Hope it helps someone else out. :)

- Evilish