Symfony3 - Error message : __construct() must be an instance of FormFactoryInterface, none given

Hello,

I’m new to symfony, and currently trying to tame the 3.1.15. Not that easy. My code tries to use a controller as a service to reuse an very simple form on any page.

An error message popped up :

Catchable Fatal Error: Argument 1 passed to AppBundle\Controller\ContactsFormController::__construct() must be an instance of FormFactoryInterface, none given, called in /mnt/400Go/www/sy1/var/cache/dev/classes.php on line 2485 and defined" at /mnt/400Go/www/sy1/src/AppBundle/Controller/ContactsFormController.php line 25

I can’t see where lays the error in my code, which is really basic. My “contacts_form_controller” is listed as a service (debug:container). Any help appreciated.

Best regards,

MC

Here is my services.yml :

# app/config/services.yml
services:
    contacts_form_controller:
        class: AppBundle\Controller\ContactsFormController
        arguments: ['@form.factory','@templating','@doctrine.orm.entity_manager']

Here is the beginning of the controller :

// src/AppBundle/Controller/ContactsFormController.php
namespace AppBundle\Controller;
/* */
use AppBundle\Entity\ContactSetClass;
use AppBundle\Form\Type\ContactsFormType;
/* */
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
/* */
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
/* */
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/* */
class ContactsFormController extends Controller
	{
	private $formFactory;
	private $templating;
	public function __construct(\FormFactoryInterface $formFactory,\EngineInterface $templating)
		{
		$this->formFactory=$formFactory;
		$this->templating=$templating;
		}
	[...]

Here is the call to the controller in another controller :

$request=new Request;
$response = $this->forward('AppBundle:ContactsForm:ContactsForm', array('request'  => $request,));

Hint:

// Symfony/Bundle/FrameworkBundle/Controller/Controller.php
protected function forward($controller, array $path = array(), array $query = array())
{
    $path['_controller'] = $controller;
    $subRequest = $this->container->get('request_stack')->
        getCurrentRequest()->duplicate($query, null, $path);

    return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}

And review how to call a controller as a service.

Ok, you saved my day once again.

In my controller as a service, I had to :

return $this->templating->renderResponse('contactsForm.html.twig',array('contactsForm' => $form->createView(),'message'=>$message,));

and not :

return $this->templating->render('contactsForm.html.twig',array('contactsForm' => $form->createView(),'message'=>$message,));

And to call and render the whole:

$response = $this->forward('contacts_form_controller:ContactsFormAction');
return $this->render('others.html.twig', array(
						'currentPage' => $currentPage." (via othersController)",
						'content' => $content,
						'contactsForm' => $response,
						));

Then, I can display the result in my page template :

{% block contactsForm %}
{{contactsForm|raw}}
{% endblock %}

The HTTP header is given along with the form (renderResponse), and render will trigger an error (not a response). If I use renderView, an error is triggered :

Attempted to call an undefined method named “renderView” of class “Symfony\Bundle\TwigBundle\TwigEngine”.

But renderView should be available, right ?

You tell me. A peek into the source code will quickly reveal the answer.

I’m having trouble understanding the context of your questions. You seem to have very definite ideas on how to organize your application but almost no background at all in php or debugging programs? The notion of rendering your form in one controller and using it in another (as opposed to just using the twig form render routines) seems strange to say the least.

I can understand wanting to customize the framework to follow your design approach. I do it myself. But first I took the time to understand the basics of php debugging as well as the basics of the framework.

You really should work through some of the examples in the docs.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.