# InputFilter factory

**URL:** <https://discourse.laminas.dev/t/inputfilter-factory/674>\
**Category:** Components & MVC\
**Created:** [August 6, 2018, 9:56am UTC](https://discourse.laminas.dev/t/inputfilter-factory/674 "2018-08-06T09:56:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbichis](https://avatars.discourse-cdn.com/v4/letter/c/dc4da7/32.png) [@cbichis](https://discourse.laminas.dev/u/cbichis)\
**Post date:** [August 6, 2018, 9:56am UTC](https://discourse.laminas.dev/t/inputfilter-factory/674/1 "2018-08-06T09:56:00Z")

</div>

Hello,

How is supposed to be created a InputFilter instance ?

Within the filters I have a RecordExists (with custom Select) and I need to pass the coresponding TableGateway/dbAdapter.

Currently to pass to controller the input filter I am getting the inputfilter from Service Manager in Controller Factory like this:

$shelfInputFilter = $container-\>get(InputFilterPluginManager::class)-\>get(ShelfInputFilter::class);

But of course, I need to pass the table gateway/dbAdapter to the input filter…

---

<div class="post-metadata">

**Author:** ![Kudo](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kudo/32/376_2.png) [@Kudo](https://discourse.laminas.dev/u/Kudo)\
**Post date:** [August 24, 2018, 3:14pm UTC](https://discourse.laminas.dev/t/inputfilter-factory/674/2 "2018-08-24T15:14:52Z")

</div>

If you now use the `InvokableFactory::class` you could register your own factory class that injects the dependencies to your object. The example below is setup for zend-servicemanager 3+. If you seek help with version 2 factories, see: [https://docs.zendframework.com/zend-servicemanager/migration/#factoryinterface](https://docs.zendframework.com/zend-servicemanager/migration/#factoryinterface)

Register the factory in your module:

```auto
 public function getInputFilterConfig()
 {
     return [
         'factories' => [
             InputFilter\ShelfInputFilter::class => TableGatewayFactory::class,
         ],
     ];
 }

```

The factory class somewhere in your application:

```auto
    class TableGatewayFactory implements FactoryInterface
    {
        /**
         * {@inheritDoc}
         */
        public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
        {
            return new $requestedName($container->get(TableGateway::class), $options);
        }
    }

```

Setup a constructor that accepts and handles your `TableGateway::class` and `$options`.

Another approach might be to create a factory for the validator class that injects the `TableGateway::class` into the validator directly. This way you don’t need to pass it along to the `InputFilter`. So when you add the validator to the InputFilter you could leave out the option and either fill it in the factory.

Why not an Initializers instead of the validator factory? I’m not a fan of that because it’s for a specific use case. Initalizers are cool and stuff but we already know by forehand that not alot of validators will receive this object, while a translator is a common case.
