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

onBeforeWrite + remove fields before saving


Reply


3 Posts   817 Views

Avatar
danzzz

Community Member, 175 Posts

6 May 2011 at 12:49am

Edited: 06/05/2011 12:49am

hi

I want to remove some fields from saving if some conditions match ... example:

public function onBeforeWrite() {

   if($this->owner->salutation == 'Ms') {
    unset ($this->owner->somefield);
   }

   parent::onBeforeWrite();
}

If users salutation = "Ms" the field "somefield" should be ignored when saving, even user changed the field ...

The sample code dont work for me.

thx

Avatar
martimiz

Forum Moderator, 1107 Posts

6 May 2011 at 6:23am

I haven't tested this in a decorator, only in a Page class, where replacing $this->somefield with $this->record['somefield'] did the trick. So maybe unset($this->owner->record['somefield']); will work...

Avatar
danzzz

Community Member, 175 Posts

7 May 2011 at 3:05am

hi

no, did not the trick in a decorator, still searching for a solution ...

thx