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.

General Questions /

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

Forms which change with urlParams['ID']


Reply


3 Posts   483 Views

Avatar
Futureweb

Community Member, 233 Posts

12 October 2012 at 4:19pm

Hi,

I have a system which lists various dataobject, these dataobjects have a 'status' and depending on the status, the form has slightly different fields. The code is below.

The problem is that when SS submits the form, it reloads the function GetItemEditForm in the browser, and doesn't have item ID in the url, and therefore doesn't have the Status, therefore doesn't load the fields, therefore the form info used to 'saveInto' is blank...

If there any way around it? i could write a different form/save function for each status, but this would be simpler...

function GetItemEditForm () {
      $id = $this->urlParams['ID'];
      $CoachingItem = DataObject::get_by_id('CoachingItem', $id);
      $itemStatus= $CoachingItem->Status;
      
      if ($itemStatus== 'Define') {
         
         $fields = new FieldSet(
            new HiddenField('MemberID','', $MemberID),
            new HiddenField('Status','', 'Define'),
            new HiddenField('ID',''),
            new TextField("Title", "My challenge"),            
            new TextAreaField("DefineGoal", "My goal, incl. time frames", '4', '')
         );
      }
      
      if ($section == 'Plan') { $fields = new FieldSet();$validator = new RequiredFields(); }
      if ($section == 'Implement') { $fields = new FieldSet();$validator = new RequiredFields(); }
      if ($section == 'Review') { $fields = new FieldSet();$validator = new RequiredFields(); }
      
      
      $actions = new FieldSet(new FormAction('SaveItemEditForm', 'Save') );
      
      $form = new Form($this, "GetItemEditForm", $fields, $actions);
      
      // load in existing content into the form
      $CoachingItem = DataObject::get_by_id('CoachingItem', $id);
      $form->loadDataFrom($CoachingItem);
      
      return $form;
      
   }
   
   function SaveItemEditForm($data, $form) {
      $item = DataObject::get_by_id('CoachingItem', $data['ID']); // loads existing item      
      $form->saveInto($item);
      $item->write();
   }

Avatar
Sam

Administrator, 685 Posts

15 October 2012 at 1:42pm

You need to make the form able to reload itself

Where you get the ID like this:

$id = $this->urlParams['ID'];

You can do it like this instead:

$id = empty($_REQUEST['ID']) ? $this->urlParams['ID'] : $_REQUEST['ID'];

That way one the reload the ID will be picked up from the hidden field you created.

Avatar
Futureweb

Community Member, 233 Posts

15 October 2012 at 2:05pm

Hi Sam: thanks - that makes sense...