简体   繁体   中英

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

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

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);

You defined your form as a service (in services.yml ) but you're not using it as a service. Instead of createForm you should use service container to create form, so change:

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

into:

$form = $this->get('apx.form.type.product')

and read more about defining forms as a service

Try in services.yml:

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

Symfony 3.4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given Catchable Fatal Error: Argument 1 passed to Foo::bar() must implement interface BazInterface, null given zend db error on switching tables Catchable fatal error: Argument 1 passed to __construct() must be an array, object given, called in Catchable Fatal Error: Argument 2 passed to UserBundle\Form\UserType::__construct() must be an instance ? Catchable Fatal Error: Argument 1 passed to AppBundle\Form\TagType::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given, Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called Symfony 2 Catchable Fatal Error: Argument 1 passed to Sg\DatatablesBundle\Datatable\::__construct() must be an instance of Catchable fatal error: Argument 1 passed to Illuminate\Config\Repository::__construct() must be of the type array, integer given Catchable Fatal Error: Argument 4 passed to UsernamePasswordToken::__construct() must be an array, null given Catchable fatal error: Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM