Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Setting a form field value based on a database entry

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

Page: 1
Go to End
Author Topic: 1261 Views
  • evilish
    Avatar
    Community Member
    9 Posts

    Setting a form field value based on a database entry Link to this post

    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

  • evilish
    Avatar
    Community Member
    9 Posts

    Re: Setting a form field value based on a database entry Link to this post

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

    Cheers,
    - Evilish

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Setting a form field value based on a database entry Link to this post

    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.

  • evilish
    Avatar
    Community Member
    9 Posts

    Re: Setting a form field value based on a database entry Link to this post

    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

    1261 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.