Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 An Invalid Factory Was Registered

I've the following classes and my module config in ZF2 application and it is giving the below error:

While attempting to create applicationformuserform(alias: Application\Form
\UserForm) an invalid factory was registered for this instance type.

UserFormFactory.php

<?php

namespace Application\Factory\Form;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\Form\UserForm;

class UserFormFactory implements FactoryInterface {

    public function createService(ServiceLocatorInterface $serviceLocator) {
        $services         = $serviceLocator->getServiceLocator();
        $entityManager    = $services->get('Doctrine\ORM\EntityManager');

        $form = new UserForm($entityManager);

        return $form;
    }
}

?>

UserForm.php

<?php

namespace Application\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
use Doctrine\ORM\EntityManager;

class UserForm extends Form implements InputFilterProviderInterface {

    protected $entityManager;

    public function __construct(EntityManager $entityManager) {
        parent::__construct();
        $this->entityManager = $entityManager;
    }

    public function init() {
        $this->add(array(
                'name' => 'username',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'User Name',
                ),
        ));
        $this->add(array(
                'name' => 'first_name',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'First Name',
                ),
        ));
        $this->add(array(
                'name' => 'last_name',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'Last Name',
                ),
        ));
        $this->add(array(
                'name' => 'role_id',
                'type' => 'DoctrineModule\Form\Element\ObjectSelect',
                'options' => array(
                        'object_manager'     => $this->entityManager,
                        'target_class'       => 'Application\Entity\Role',
                        'property' => 'id',
                        'is_method' => true,
                        'find_method'        => array(
                                'name'   => 'getRoles',
                        ),
                        'label' => 'User Role',
                ),
        ));
    }

    public function getInputFilterSpecification() {
        return array(); // filter and validation here
    }
}

?>

Module.config.php

'form_elements' => array(
            'factories' => array(
                    'Application\Form\UserForm' => 'Application\Factory\Form\UserFormFactory',
            ),
    ),

And I'm using this form factory in another controller factory

UserControllerFactory.php

<?php

namespace Member\Factory\Controller;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Member\Controller\UserController;
use Application\Form\UserForm;

class UserControllerFactory implements FactoryInterface {

    public function createService(ServiceLocatorInterface $serviceLocator) {
        $services    = $serviceLocator->getServiceLocator();
        $userForm    = $services->get('FormElementManager')->get('Application\Form\UserForm');

        $controller  = new UserController($userForm);

        return $controller;
    }
}

?>

Could anybody tell me that what may be the issue?

like image 430
Manish Jangir Avatar asked Mar 19 '15 14:03

Manish Jangir


3 Answers

Your Factory is not being found.

Check if you using PSR-4 or PSR-0 in your controller among the other answers

Briefly

  • did you name the Factory correctly (no misspellings)?
  • Is your composer.json updated with PSR-0, or PSR-4 namespaces for your modules?
  • did you run composer dump-autoload?
  • Does your autoload_classmap.php contain an outdated entry & confuses autoloader?
  • Check your folder structure and names
  • make sure your Factory implements FactoryInterface

Ask yourself "Why is my Factory class not being found when I have placed it right there" where it obviously without a doubt must be found? That will help you guide your way into finding out what's wrong.

like image 74
dennismv Avatar answered Nov 14 '22 06:11

dennismv


I got my answer at my own after looking at the code again and again. Actually my Factory and Form folders were outside of src folder that's why Zend could not found all the classes of both folders.

I moved both Factory and Form folder in src and now it's working fine.

like image 4
Manish Jangir Avatar answered Nov 14 '22 07:11

Manish Jangir


I had a similar problem. I had made some changes to my factory classes (refactoring + minor class name changes). Turns out that because I was using the Classmap Autoloader ... and forgot to re-run php vendor/bin/classmap_generator.php in the module structure ... the newly renamed classes were not found. Too bad a "class not found" error wasn't generated.

like image 1
dougB Avatar answered Nov 14 '22 05:11

dougB