I'm finding it inexplicably hard to create a “edit” form. The approach I’m taking is to use an argument in the URL to pass the ID of an “AutoMap” Dataobject and then load that into an edit form.
The code below works for the AutoMap_Contoller “form” action, but not the “editform”. It returns the error that the “Call to a member function allParams() on a non-object”. However, even if I mess around with the code and replace it with a hardcode ID, the system runs out of php memory, so there must be something else fundamentally broken in my code.
class AutoMap_Controller extends Page_Controller {
private static $allowed_actions = array('form', 'editform');
public function form() {
$fields = new FieldList(
new TextField('Name', 'Name', $fieldvalue->name),
new TextareaField('Code', 'Your Code', $fieldvalue->code),
new TextField('StringList', 'String List')
);
$actions = new FieldList(
new FormAction('submit', 'Submit')
);
$validator = new RequiredFields('Name', 'Code');
$form = new Form($this, 'Form', $fields, $actions, $validator);
return $form;
}
public function editform(SS_HTTPRequest $request) {
$arg = $request->allParams();
$AutoMap = AutoMap::get()->byID($arg['ID']);
$fields = new FieldList(
new TextField('Name', 'Edit Name'),
new TextareaField('Code', 'Edit Code'),
new TextField('StringList', 'Edit String List')
);
$actions = new FieldList(
new FormAction('submit', 'Submit')
);
$editform = new Form($this, 'editform', $fields, $actions);
$editform->loadDataFrom($AutoMap);
return array(
'Form' => $this->editform()
);
return $form;
}
public function submit($data, $form) {
$newAutoMap = new AutoMap();
$newAutoMap->name = $data['Name'];
$newAutoMap->code = $data['Code'];
$newAutoMap->stringlist = $data['StringList'];
$newAutoMap->enabled = "Disabled";
$newAutoMap->enabled = "Disabled";
$newAutoMap->write();
return array(
'Content' => '<p>Automatic Mapping '.$newAutoMap->ID.' Added</p>',
'Form' => ''
);
}
}