Jump to:

23361 Posts in 18124 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » onBeforeWrite + remove fields before saving

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: 762 Views
  • danzzz
    Avatar
    Community Member
    175 Posts

    onBeforeWrite + remove fields before saving Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: onBeforeWrite + remove fields before saving Link to this post

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

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: onBeforeWrite + remove fields before saving Link to this post

    hi

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

    thx

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