I have a Form, FormFactory and and InputFilter. I want to instantiate a form and assign input filter to the form on createService() method.
Module has getFormElementConfig() method and getInputFilterConfig()
When I try to access $serviceManager->get('InputFilterManager')->get('Page\Form\NewsFormInputFilter') from form factory, I get following error mesage
Invalid filter specification provided; was neither a filter instance nor an array specification
module/Page/Module.php
<?php
namespace Page;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getFormElementConfig()
{
return array(
'factories' => array(
'Page\Form\NewsForm' => 'Page\Form\NewsFormFactory',
)
);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
)
)
);
}
public function getInputFilterConfig()
{
return array(
'invokables' => array(
'Page\Form\NewsFormInputFilter' => 'Page\Form\NewsFormInputFilter'
)
);
}
}
module/Page/src/Page/Form/NewsFormFactory.php
<?php
namespace Page\Form;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\MutableCreationOptionsInterface;
use Page\Form\NewsForm;
class NewsFormFactory implements FactoryInterface, MutableCreationOptionsInterface
{
protected $options;
public function setCreationOptions(array $options)
{
$this->options = $options;
}
public function createService(ServiceLocatorInterface $serviceLocator)
{
$serviceManager = $serviceLocator->getServiceLocator();
$form = new NewsForm();
$form->setTranslator($serviceManager->get('translator'));
$form->setInputFilter($serviceManager->get('InputFilterManager')->get('Page\Form\NewsFormInputFilter'));
return $form;
}
}
module/Page/src/Page/Form/NewsForm.php
<?php
namespace Page\Form;
use Zend\Form\Element;
use Zend\InputFilter;
use Zend\Form\Form;
use Zend\Form\Element\Select;
use Zend\InputFilter\InputFilterInterface;
use Zend\I18n\Translator\TranslatorAwareTrait;
class NewsForm extends Form
{
use TranslatorAwareTrait;
public function __construct($name = null)
{
parent::__construct('News');
}
public function init()
{
$category = new Select('category');
$category->setValueOptions($this->getNewsCategoryList());
$this->add($category);
}
private function getNewsCategoryList()
{
// implementation
}
}
module/Page/src/Page/Form/NewsFormInputFilter.php
<?php
namespace Page\Form;
use Zend\Filter\StringTrim;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\InputFilter\InputFilter;
use Zend\Form\Element;
class NewsFormInputFilter extends InputFilter
{
public function init()
{
$this->add(
array(
'name' => 'category',
'required' => true,
'filters' => array(
'name' => 'StringTrim'
)
)
);
}
}
According to Module Manager Listeners, getInputFilterConfig() is available in Module.php
I'm not sure what I'm doing wrong. Any idea is welcome.
Edit:
According to InputFilter and explained as @Purple Hexagon I have removed InputFilterProviderInterface implementation from NewsFormInputFilter and extended only from InputFilter
Also removed duplicate implementation of $inputFilter property and getter method from NewsForm
Most likely you have a configuration issue. I think this line is most likely not returning anything:
I see you declared
'Page\Form\NewsFormInputFilter'in your Module.phpgetInputFilterConfig()method. But I don't see thisInputFilterin your code. Do you have a file for that class?You should have an input filter class named
NewsFormInputFilterin the namespacePage\Form.UPDATE
Try once to remove the
getInputFilterSpecificationand don't implement theInputFilterProviderInterface. It seems to me that you wantinitto be called. You don't need the interface and thegetInputFilterSpecificationmethod for that.Make sure your module class implements
Zend\ModuleManager\Feature\InputFilterProviderInterface.Are you sure your files are in the
Pagemodule?