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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Filling getFrontendFields with a DataObject's data


Go to End


4 Posts   4411 Views

Avatar
anselmdk

Community Member, 18 Posts

25 February 2010 at 10:10pm

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;

}

Avatar
Judge

Community Member, 79 Posts

5 March 2010 at 4:39am

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

Avatar
anselmdk

Community Member, 18 Posts

25 March 2010 at 9:17am

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

Avatar
javelin

Community Member, 12 Posts

1 June 2010 at 6:35pm

$form->loadDataFrom($event);

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