Reputation: 11
Fatal error: Call to a member function Services() on null
class MemberProfileViewer extends Page_Controller { // ... public function Servizi() { $source = null; if ($id = (int)$this->urlParams['ID']) { $member = Member::get()->byID($id); if ($member instanceof Member) { return $source = $member->Services()->map('Title','Title'); } } if (is_null($id)) { return $this->redirect('/'); } if (is_null($source)) { return $this->httpError(404); } } public function form() { $fields = new FieldList( CheckboxSetField::create( $name = "ServiceID", $title = "Select", $source = $this->Servizi() ), ) $actions = FieldList::create( FormAction::create('form', 'Send') ->setUseButtonTag(true) ->addExtraClass('btn btn-primary') ); $validator = new RequiredFields('ServiceID'); $form = new Form($this, 'form', $fields, $actions, $validator); $form->setFormMethod('POST', true); return $form; } public function doform($data, $form) { $form->sessionMessage( "Thank you, you will be contacted as soon as possible by our consultant.", 'good' ); $submission = new RequestForm(); $form->saveInto($submission); $submission->write(); return $this->redirectBack(); } }
is always null
I do not know how to solve
class MemberProfileViewer extends Page_Controller { // ... public function currentPageID() { $id = 0; $request = $this->getRequest(); if(is_numeric($request->requestVar('ID'))) { $id = $request->requestVar('ID'); } elseif (is_numeric($request->param('ID'))) { $id = $request->param('ID'); } $id = (int)$id; return $id; } public function Servizi() { $source = null; if ($id = $this->currentPageID()) { Session::set('ID', $id); return $source = Member::get()->byID($id)->Services()->map('Title','Title')->toArray(); } elseif (is_null($source)) { return $source = Member::get()->byID(Session::get('ID'))->Services()->map('Title','Title')->toArray(); } } public function form() { $fields = new FieldList( CheckboxSetField::create( $name = "Service", $title = "Select", $source = $this->Servizi() ), ) $actions = FieldList::create( FormAction::create('form', 'Send') ->setUseButtonTag(true) ->addExtraClass('btn btn-primary') ); $validator = new RequiredFields('Service'); $form = new Form($this, 'form', $fields, $actions, $validator); $form->setFormMethod('POST', true); $data = Session::get("FormData.{$form->getName()}.data"); return $data ? $form->loadDataFrom($data) : $form; } public function doform($data, $form) { Session::set("FormData.{$form->getName()}.data", $data); $submission = new RequestForm(); $form->saveInto($submission); $submission->write(); $form->sessionMessage( "Thank you, you will be contacted as soon as possible by our consultant.", 'good' ); Session::clear("FormData.{$form->getName()}.data"); return $this->redirectBack(); } }
Upvotes: 0
Views: 409
Reputation: 757
I would do:
$source = null;
if ($id = $this->urlParams['ID']) {
$member = Member::get()->byId($id);
if ($member instanceof Member) {
$source = $member->Services()->map('Title','Title');
}
}
if (is_null($source)) {
//don't show that field, show a LiteralField with an error maybe
} else {
//show your field with $source
}
Upvotes: 0
Reputation: 1024
Your problem is probably because of this line
$fields = new FieldList(
CheckboxSetField::create(
$name = "ServiceID",
$title = "Select",
$source = $this->Servizi() // <--
),
)
Your method Servizi()
depends on some routing (you try to get a Member ID from the URL). The method does not always return a good map for your CheckboxSetField. If you don't have a route for your Servizi()
method, you probably don't want to redirect or return a http error.
Now I don't know how you got your Member ID so that is up to you how you will get that. But you will be able to fix it like this.
public function servizi($memberId = 0)
{
$array = [];
if(! $memberId && ! ($memberId = Member::currentUserID())) // check current logged in Member
return $array;
if(! $member = Member::get()->byID($memberId))
return $array;
return $member->Services()->map('Title','Title');
}
The code above is not tested.
Upvotes: 0