Jump to:

22999 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Filling getFrontendFields with a DataObject's data

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: 2132 Views
  • anselm.dk
    Avatar
    Community Member
    18 Posts

    Filling getFrontendFields with a DataObject's data Link to this post

    I've just started to use the getFrontendFields method on the frontend of my sites, and it really increases time when setting up an add form like this:

       function AddEventForm(){
          $fields = singleton('MemberEventDo')->getFrontendFields();
          $actions = new FieldSet(
             new FormAction('doAddEvent', 'Tilføj Aftale')
          );

          $validator = NULL;      
          $form = new Form(
             $this,
             'AddEventForm',
             $fields,
             $actions,
             $validator
          );
          return $form;

       }

    Now I'm wondering (as I'd like to do the same with an edit form), how to fill the generated form with the DataObject's fields. For me the obvioius would be the following (which unfortunately leaves me with an empty form):

       function EditEventForm(){
          $eventID = (int) Director::urlParam("ID");
          $event = DataObject::get_by_id('MemberEventDo',$eventID);
          
          echo $event->Title; //just for checking that I'm actually having loaded a DataObject

          $fields = $event->getCMSFields($event);

          $actions = new FieldSet(
             new FormAction('doEditEvent', 'Redigér Aftale')
          );

          $validator = NULL;      
          $form = new Form(
             $this,
             'EditEventForm',
             $fields,
             $actions,
             $validator
          );
          return $form;

       }

  • Judge
    Avatar
    Community Member
    79 Posts

    Re: Filling getFrontendFields with a DataObject's data Link to this post

    Just got stuck on this myself, but in the admin back-end screens. This seems to work for me:

    $fields->setValues($event->getAllFields());

    That gets all the values from the event object into an array, and then passes them into the fields in the fieldset.

    -- Jason

  • anselm.dk
    Avatar
    Community Member
    18 Posts

    Re: Filling getFrontendFields with a DataObject's data Link to this post

    Great.
    I just checked up on it, and it works!

  • javelin
    Avatar
    Community Member
    12 Posts

    Re: Filling getFrontendFields with a DataObject's data Link to this post

    $form->loadDataFrom($event);

    does the same on the form, after it has been constructed.

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