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