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.

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

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

[SOLVED] Readonly fields being saved as empty


Reply


3 Posts   945 Views

Avatar
blueskies

Community Member, 42 Posts

12 February 2013 at 12:28am

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?

Avatar
Van

Administrator, 25 Posts

18 February 2013 at 1:19pm

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.

Avatar
blueskies

Community Member, 42 Posts

23 April 2013 at 9:39pm

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.