ZF2 - Custom Validator via factory
<p>I cant load my <strong>CustomValidator</strong> via factory:</p>

<pre><code>namespace My\Validator;
class CustomValidator extends AbstractValidator
{
public function __construct(EntityManager $em, AuthenticationService $auth)
{
    $this-&gt;auth = $auth;
    $this-&gt;em = $em;

    parent::__construct();
}
</code></pre>

<p><strong>Module (My) Config</strong>:</p>

<pre><code>namespace My;
...
'service_manager' =&gt; array(
  'abstract_factories' =&gt; array(
    'Application\Factory\FormAbstractFactory'
  ),
),
'controllers' =&gt; array(
    'factories' =&gt; array(
        'My\Controller\CustomController' =&gt; 'My\Factory\Controller\CustomControllerFactory',
    ),
),
'validators' =&gt; array(
    'factories' =&gt; array(
        'My\Validator\CustomValidator' =&gt; 'My\Factory\Validator\CustomValidatorFactory'
    ),
),
</code></pre>

<p><strong>CustomForm</strong>:</p>

<pre><code>namespace My\Form;
class CustomForm extends SearchForm implements InputFilterProviderInterface {
    public function __construct(ObjectManager $objectManager, Translator $translator) {
        parent::__construct ($objectManager, $translator,  "customForm" );
        // Here are some elements added...
        $this-&gt;add(array("name" =&gt; "search", /* some more properties */));
    }

    public function getInputFilterSpecification()
    {
        return array(
            'search' =&gt; array(
                'validators' =&gt; array(
                    array(
                        'name' =&gt; 'My\Validator\CustomValidator'
                    )
                )
            )
        );
    }
}
</code></pre>

<p><strong>CustomValidatorFactory</strong>:</p>

<pre><code>namespace My\Factory\Validator;
class CustomValidatorFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator)
{
    $sl = $serviceLocator-&gt;get("service_manager");
    $entityManager = $sl-&gt;get('doctrine.entitymanager.orm_default');
    $auth = $sl-&gt;get('zfcuser_auth_service');

    return new CustomValidator($entityManager, $auth);
}
</code></pre>

<p>I get a PHP error saying that __construct's first parameter ($em) must be an instance of EntityManager, none given...
My Factory is not instantiated... I think my CustomValidator is still created by some other factory (or ValidatorPluginManager?)...
How do I have to change my code to get my CustomValidatorFactory working?</p>

<p><strong>EDIT</strong> - complete Exception by rendering view with form which has my custom validator attached:</p>

<pre><code>( ! ) Fatal error: Uncaught TypeError: Argument 1 passed to My\Validator\CustomValidator::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /xxxxxx/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php on line 252 and defined in /xxxxxx/module/My/src/My/Validator/CustomValidator.php on line 32
( ! ) TypeError: Argument 1 passed to My\Validator\CustomValidator::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /xxxxxx/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php on line 252 in /xxxxxx/module/My/src/My/Validator/CustomValidator.php on line 32
Call Stack
#   Time    Memory  Function    Location
1   0.0001  369208  {main}( )   .../index.php:0
2   0.1165  11279664    Zend\Mvc\Application-&gt;run( )    .../index.php:28
3   0.2780  17578688    Zend\Mvc\Application-&gt;completeRequest( )    .../Application.php:356
4   0.2780  17578688    Zend\EventManager\EventManager-&gt;triggerEvent( ) .../Application.php:384
5   0.2780  17578688    Zend\EventManager\EventManager-&gt;triggerListeners( ) .../EventManager.php:251
6   0.2781  17580376    call_user_func:{/xxxxxx/vendor/zendframework/zend-eventmanager/src/EventManager.php:490} ( )    .../EventManager.php:490
7   0.2781  17580376    Zend\Mvc\View\Http\DefaultRenderingStrategy-&gt;render( )  .../EventManager.php:490
8   0.2781  17580376    Zend\View\View-&gt;render( )   .../DefaultRenderingStrategy.php:103
9   0.2786  17591848    Zend\View\View-&gt;renderChildren( )   .../View.php:200
10  0.2787  17592840    Zend\View\View-&gt;render( )   .../View.php:236
11  0.2789  17593272    Zend\View\Renderer\PhpRenderer-&gt;render( )   .../View.php:207
12  0.2792  17633952    include( '/xxxxxx/module/My/view/my/view/index.phtml' ) .../PhpRenderer.php:502
13  0.2810  17743208    Zend\Form\Form-&gt;prepare( )  .../index.phtml:13
14  0.2810  17743208    Zend\Form\Form-&gt;getInputFilter( )   .../Form.php:203
15  0.2819  17885496    Zend\Form\Form-&gt;attachInputFilterDefaults( )    .../Form.php:696
16  0.2836  18054248    Zend\InputFilter\Factory-&gt;createInput( )    .../Form.php:801
17  0.2837  18054888    Zend\InputFilter\Factory-&gt;populateValidators( ) .../Factory.php:265
18  0.2837  18054944    Zend\Validator\ValidatorChain-&gt;attachByName( )  .../Factory.php:408
19  0.2837  18054944    Zend\Validator\ValidatorChain-&gt;plugin( )    .../ValidatorChain.php:194
20  0.2837  18057312    Zend\ServiceManager\AbstractPluginManager-&gt;get( )   .../ValidatorChain.php:97
21  0.2839  18068224    Zend\ServiceManager\ServiceManager-&gt;get( )  .../AbstractPluginManager.php:161
22  0.2839  18068920    Zend\ServiceManager\ServiceManager-&gt;create( )   .../ServiceManager.php:532
23  0.2839  18068920    Zend\ServiceManager\ServiceManager-&gt;doCreate( ) .../ServiceManager.php:599
24  0.2839  18068920    Zend\ServiceManager\AbstractPluginManager-&gt;createFromInvokable( )   .../ServiceManager.php:640
25  0.2839  18069160    My\Validator\CustomValidator-&gt;__construct( )    .../AbstractPluginManager.php:252
</code></pre>

<p><strong>EDIT2</strong> - <strong>FormAbstractFactory</strong>:</p>

<pre><code>class FormAbstractFactory implements AbstractFactoryInterface {

    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        return (fnmatch('*Form', $requestedName)) ? true : false;
    }

    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        if (class_exists($requestedName)) {
            $entityManager = $serviceLocator-&gt;get('doctrine.entitymanager.orm_default');
            $translator    = $serviceLocator-&gt;get('translator');

            return new $requestedName($entityManager, $translator);
        }

        return false;
    }

}
</code></pre>

<p><strong>CustomControllerFactory:</strong></p>

<pre><code>namespace My\Factory\Controller;
use My\Controller\CustomController;
class CustomControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $realServiceLocator = $serviceLocator-&gt;getServiceLocator();
        $entityManager      = $realServiceLocator-&gt;get('doctrine.entitymanager.orm_default');
        // Created through FormAbstractFactory
        $customForm          = $realServiceLocator-&gt;get('My\Form\CustomForm');
        $orderByService     = $realServiceLocator-&gt;get('Application\Service\OrderByServiceAbstract');

        return new CustomController($entityManager, $customForm, $orderByService);
    }
}
</code></pre>

<p><strong>CustomController</strong></p>

<pre><code>namespace My\Controller;

class CustomController extends AbstractActionController
{
    public function __construct(ObjectManager $entityManager, CustomForm $customForm, OrderByService $orderByService)
    {
        $this-&gt;em = $entityManager;
        $this-&gt;form = $customForm;
        $this-&gt;orderByService = $orderByService;
    }

    public function indexAction()
    {
        // some code...

        $vm = new ViewModel (array(
            "form" =&gt; $this-&gt;form
        ));

        $vm-&gt;setTemplate("my/custom/index");

        return $vm;
    }
}
</code></pre>

<p><strong>my/custom/index - View:</strong></p>

<pre><code>&lt;?php
namespace My;
$form = $this-&gt;form;
$form-&gt;prepare(); ?&gt;
&lt;!-- HTML Stuff... -&gt;
&lt;?php echo $this-&gt;form($form, null); ?&gt;
&lt;!-- HTML Stuff... -&gt;
</code></pre>

<p>Well I am using a custom form-renderer too - <a href="https://github.com/neilime/zf2-twb-bundle" rel="nofollow">https://github.com/neilime/zf2-twb-bundle</a>.</p>

<p>I filed an issue at zf2's git: <a href="https://github.com/zendframework/zf2/issues/7688" rel="nofollow">https://github.com/zendframework/zf2/issues/7688</a></p>