Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catchable Fatal Error: Argument 1 passed to "...\FormType::__construct() must implement interface

I am trying to call entityManager in formType. I don't get why this is not working.

FormType :

private $manager;

public function __construct(ObjectManager $manager)
{
    $this->manager = $manager;
}

Controller:

$form = $this->createForm(ProductsType::class, $products);

Services:

apx.form.type.product:
    class: ApxDev\UsersBundle\Form\ProductType
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: form.type }

Error:

Catchable Fatal Error: Argument 1 passed to MyBundle\Form\FormType::__construct() must implement interface Doctrine\Common\Persistence\ObjectManager, none given, called in vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 90 and defined

like image 722
Nicks Avatar asked Dec 01 '15 13:12

Nicks


2 Answers

Assuming your services.yml file is being loaded and that you copied pasted in the contents then you have a simple typo:

# services.yml
class: ApxDev\UsersBundle\Form\ProductType
should be
class: ApxDev\UsersBundle\Form\ProductsType
like image 162
Cerad Avatar answered Oct 14 '22 03:10

Cerad


Let's look at your error

Argument 1 passed to MyBundle\Form\FormType::__construct()

So when you're instantiating FormType we're talking about the argument you pass like so

$form = new \MyBundle\Form\FormType($somearg);

Your definition says

public function __construct(ObjectManager $manager)

Based on the second part of the error

must implement interface Doctrine\Common\Persistence\ObjectManager

it's obvious that ObjectManager is an interface. So what that means is you have to implement that interface in the object you're injecting into your class because that's what you told PHP to expect. What that looks like is this

class Something implements \Doctrine\Common\Persistence\ObjectManager {
    // Make sure you define whatever the interface requires within this class
}
$somearg = new Something();
$form = new \MyBundle\Form\FormType($somearg);
like image 35
Machavity Avatar answered Oct 14 '22 04:10

Machavity