Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Setting dynamic default values with ModelAdmin

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

Page: 1 2
Go to End
Author Topic: 814 Views
  • NETim
    Avatar
    Community Member
    23 Posts

    Setting dynamic default values with ModelAdmin Link to this post

    Hi,

    Been fighting with this for a little while now.

    I have put together a very basic CRM in SS using ModelAdmin where the details from a contact form are stored in the DB and shown under a tab section called CRM. What I'm trying to do with it now is to add a notes section where multiple notes can be stored with the date they were added and the member that added them.

    The 'note' field works fine, as do the 'Date' and 'Member' fields. The problem comes when I try and set default values for the last 2 fields.

    I have dug around the forums and a host of other sites for this and can't find anything about doing this with ModelAdmin.

    Any help would be great.

    T.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Setting dynamic default values with ModelAdmin Link to this post

    Hi NETim,

    Welcome to the forums!

    I don't really follow...

    The 'note' field works fine, as do the 'Date' and 'Member' fields. The problem comes when I try and set default values for the last 2 fields

    are date and member the last two fileds?

    first date is superflous because every record has a created and lasted edited field within it.
    second the Member could be addin like follows..

       public function onBeforeWrite() {
          parent::onBeforeWrite();

          $this->MemberID = Member::currentUserID();
       }

  • NETim
    Avatar
    Community Member
    23 Posts

    Re: Setting dynamic default values with ModelAdmin Link to this post

    swaiba,

    Thanks.

    I had seen a few people mention the date fields but haven't had any success in using them.

    With the function you listed, how would it be called in the context of ModelAdmin. Would I need to call it or would it be called when saving a record?

    The basic idea of what I am trying to achieve is thus:

    - A visitor to the site fills in the contact form
    - When they submit the form, the values from the form are stored as 'SubmittedForm'
    - In the CMS there is a new ModelAdmin which manages the 'SubmittedForm' model
    - each 'SubmittedForm' then has many 'Notes'
    - each 'Note' should have: 'Content', 'CreatedDate' and 'MemberName'

    The only part of this that is not working as I intended is pre-populating the 'Date' and 'MemberName' fields in the pop-up when a user adds a new 'Note'.

    The reason I am trying to pre-populate is to ensure the integrity of the data, so that we can be sure of exactly when, and by whom, the note was added without having to trust the user not to have entered false data.

    Thanks

    T.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Setting dynamic default values with ModelAdmin Link to this post

    the onBeforeWrite is a function to be placed in your Notes DataObject.
    to see the created date (that the system writes automatically) this will help...

    http://www.silverstripe.org/data-model-questions/show/18942

  • NETim
    Avatar
    Community Member
    23 Posts

    Re: Setting dynamic default values with ModelAdmin Link to this post

    Thanks,

    That handles the after insertion part.

    Any ideas on pre-populating those values when adding the note? The fields are editable when you add a new note and my intention is to have them set with the values to begin with.

    If it's not possible then I can always go with hiding them from the form and just set them in the background.

    Thanks

    T.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Setting dynamic default values with ModelAdmin Link to this post

    use getCMSFields
    but I hide them - that way it doens't even give the user the ilusion they can modifiy it
    and ensure you always get teh right memberid/created date instead of the one they want to add

  • NETim
    Avatar
    Community Member
    23 Posts

    Re: Setting dynamic default values with ModelAdmin Link to this post

    Thanks,

    You raise a good point.

    My next issue would have been disabling the fields anyway. This way seems simpler. Less points at which it could go wrong.

    Cheers,

    T.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Setting dynamic default values with ModelAdmin Link to this post

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->removeByName('FIELDNAMEHERE');

          return $fields;
       }

    814 Views
Page: 1 2
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.