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.

Data Model Questions /

Setting dynamic default values with ModelAdmin


Go to End
Reply


10 Posts   1083 Views

Avatar
NETim

Community Member, 24 Posts

12 January 2012 at 1:53am

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.

Avatar
swaiba

Forum Moderator, 1804 Posts

13 January 2012 at 6:27am

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();
   }

Avatar
NETim

Community Member, 24 Posts

13 January 2012 at 10:27pm

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.

Avatar
swaiba

Forum Moderator, 1804 Posts

13 January 2012 at 10:30pm

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

Avatar
NETim

Community Member, 24 Posts

14 January 2012 at 12:12am

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.

Avatar
swaiba

Forum Moderator, 1804 Posts

14 January 2012 at 12:27am

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

Avatar
NETim

Community Member, 24 Posts

14 January 2012 at 4:51am

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.

Avatar
swaiba

Forum Moderator, 1804 Posts

14 January 2012 at 4:56am

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

      return $fields;
   }

Go to Top