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

Form with readonly-fields overwriting existing values


Reply


3 Posts   900 Views

Avatar
blueskies

Community Member, 42 Posts

14 March 2013 at 12:14am

HI all,

Hope someone can point me in the right direction with this problem. I have set up some forms in my front end to add/edit dataobjects. The idea is that with "add" all from fields are editable and with an "edit" only certain fields are editable. When I set some of the fields in the edit form to readonly, those fields get set to NULL/0 after the dataobject->write()... That is incorrect, they should be left alone and keep their original (add) value. This used to work in SS 2.* but readonly's get changed to 0/NULL when I upgraded to 3.

I have a form set up, the standard way:

public function EditForm() {         
      $fields = $this->currentRecord->getFrontendFields($this->request->param('OtherID'));      
      $validator = ($this->currentRecord->hasMethod('getValidator')) ? $this->currentRecord->getValidator() : new RequiredFields();
      $actions = new FieldList(
         new FormAction("doEdit", 'Opslaan')
      );      
      $form = new Form($this, "EditForm", $fields, $actions, $validator);
      $form->loadDataFrom($this->currentRecord);
      return $form;
   }

with a form handler:

function doEdit($data, $form, $request) {
      $form->saveInto($this->currentRecord);
      $this->currentRecord->write();
      return "#".$CSSClassName.$this->currentRecord->ID;
   }

When I add a dataobject, all form fields are editable. When a dataobject is edited, only certain fields can be changed. I handle that in the getFrontEndFields of the dataobject. Ie:

function getFrontendFields($actionGroup='edit') {
   $VerbruikKwhUrenHoog = new TextField('Verbruik Kwh Uren Hoog','Verbruik Kwh uren hoog(T1)');
   $VerbruikKwhUrenLaag = new TextField('Verbruik Kwh Uren Laag','Verbruik Kwh uren laag (T1)');
   if( !empty($this->ID) ){
      $VerbruikKwhUrenLaag = $VerbruikKwhUrenLaag->performReadonlyTransformation();   
   }      
   return new FieldList(
      new FieldGroup(
         $VerbruikKwhUrenLaag,
         $VerbruikKwhUrenHoog
      )         
   );
}   

Now after the $this->currentRecord->write() in the doEdit method all the readonly fields are set to 0/NULL. Is this expected behaviour since SS3? Or am I missing something?

Avatar
Willr

Forum Moderator, 5513 Posts

20 March 2013 at 8:39pm

Sounds like a 'feature' that wasn't documented in 2.* and has been removed. If a field is a Readonly field it shouldn't update the object. Perhaps raise a ticket to open.silverstripe.org.

Avatar
blueskies

Community Member, 42 Posts

19 April 2013 at 8:51am

Hmmm... I was afraid of that. Let me find a clean test-case and possibly raise a ticket.