Jump to:

1131 Posts in 1965 Topics by 402 members

Upgrading SilverStripe

SilverStripe Forums » Upgrading SilverStripe » [SOLVED] Readonly fields being saved as empty

Ask questions about upgrading SilverStripe to the latest version.

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

Page: 1
Go to End
Author Topic: 746 Views
  • blueskies
    Avatar
    Community Member
    42 Posts

    [SOLVED] Readonly fields being saved as empty Link to this post

    I have the following situation: In a DataObject I have a list of formfields in getFrontendFields of which some are readonly (performReadonlyTransformation) when _updating_ the dataobject. So when I _add_ a dataobject I can fill in things like ManufacturedDate and when I _edit_ the DataObject the ManufacturedDate is readonly but other fields (such as Status) are editable.

    The problem is, is that when I change the Status during _editing_, the ManufacturedDate is reset to NULL. Even though I filled in 2013 when I _added_ the dataobject, when I edit it and _only_ change the Status (and ManufacturedDate is readonly) the ManufacturedDate gets saved/reset to NULL.

    This was NOT the case in Silverstripe 2.*. It has only been happening since 3.*. What is best practice to prevent this from happening? Do I really need to add a hidden field with the current ManufacturedDate in it? That seems so messy. Somehow I'm missing something in the docs... can someone please point me in the right direction?

  • Van
    Avatar
    Administrator
    25 Posts

    Re: [SOLVED] Readonly fields being saved as empty Link to this post

    Hi blueskies,

    That's a tough one. It might help if you could post some of your code because it shouldn't be doing that.

    Perhaps look over the bug tracker to see if anyone else is having that problem. Sorry I can't help any more than that.

  • blueskies
    Avatar
    Community Member
    42 Posts

    Re: [SOLVED] Readonly fields being saved as empty Link to this post

    I've found out why this is happening, and am wondering why this hasn't been raised as a major issue before.

    I found: API CHANGE Read-only fields no longer include companion hidden fields…
    https://github.com/silverstripe/sapphire/commit/3b3b515571ba54cf8ca9c4dc1deaa82210e04d88

    And because those hidden fields are no longer included my entire application (which was built upon generic views) was saving incorrectly.

    If I used:

    $Hoogte = new NumericField('Hoogte','Hoogte (mm)', $this->Hoogte);
    if($actionGroup == 'edit'){
    $Hoogte = $Hoogte->performReadonlyTransformation();
    }   


    ... it would resave the dataobject with "Hoogte"=0, even if the "Hoogte" was 250 or whatever other number.   

    I can fix it by using this:

    $Hoogte = new NumericField('Hoogte','Hoogte (mm)', $this->Hoogte);
    if($actionGroup == 'edit'){
    $Hoogte = $Hoogte->performReadonlyTransformation();
    $Hoogte->setIncludeHiddenField(true);
    }   

    ... but am not happy with this, as I cannot possibly change every singly form by hand (the app currently has 54 dataobjects), so I'm trying to find a way to maybe override the ReadOnly object. In the meantime: anyone running into this can find this as the answer and hopefully not spend as many days on it as I have due to lacking documentation.

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