Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Forms which change with urlParams['ID']

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

Page: 1
Go to End
Author Topic: 413 Views
  • Futureweb
    Avatar
    Community Member
    232 Posts

    Forms which change with urlParams['ID'] Link to this post

    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();
       }

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Forms which change with urlParams['ID'] Link to this post

    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.

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: Forms which change with urlParams['ID'] Link to this post

    Hi Sam: thanks - that makes sense...

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