Jump to:

1161 Posts in 2632 Topics by 409 members

Upgrading SilverStripe

SilverStripe Forums » Upgrading SilverStripe » Form with readonly-fields overwriting existing values

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: 823 Views
  • blueskies
    Avatar
    Community Member
    42 Posts

    Form with readonly-fields overwriting existing values Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Form with readonly-fields overwriting existing values Link to this post

    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.

  • blueskies
    Avatar
    Community Member
    42 Posts

    Re: Form with readonly-fields overwriting existing values Link to this post

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

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