Reputation: 219
I want to call method inside class that creates form. But I get this error.
Error: Call to a member function get() on null
This is class that should handle form operations.
class CommentController extends Controller
{
public function replyAction()
{
$comment = new Comments();
$form = $this->createFormBuilder($comment)
->add('name', TextType::class)
->add('text', TextType::class)
->add('reply', SubmitType::class, array(
'label' => 'Reply'))
->getForm();
return $this->$form;
}
}
and this is how i Call the method from another class
$form = (new CommentController())->replyAction();
It works if I place the replyAction
code into class that I am calling it from, but I want to have it inside separate class.
Upvotes: 0
Views: 3187
Reputation: 48893
To answer your actual question, the reason you are getting the error is because something called the container is not being injected into your controller. In theory you could fix this with something like:
// SomeOtherClass
$commentController = new CommentController();
$commentController->setContainer($container);
$form = $commentController->replyAction(); // Assuming replyAction has 'return $form;'
Needless to say, your SomeOtherClass may or may not have access to the container.
The real question of course is why you are trying to do such a thing? Because quite frankly, it does not make much sense. It is not uncommon for developers to mix up controllers with services and what not.
Consider updating your question with an explanation of what you trying to do. Create a form via a service perhaps?
Upvotes: 1