Closed cbichis closed 6 years ago
Btw, this is NOT a Doctrine related issue, if I create a factory for RecordExists validator I am getting through same issue.
Having same issue... In my form:
public function getInputFilterSpecification()
{
return array(
'search' => array(
'validators' => array(
array(
'name' => 'My\Validator\CustomValidator'
)
)
)
);
}
In my module.config.php:
'validators' => array(
'factories' => array(
'My\Validator\CustomValidator' => 'My\Factory\Validator\CustomValidatorFactory'
),
),
Factory is never used. The validator is still invoked... Maybe a bug?
I guess that this might be related to not initializing the ValidatorChain and/or its PluginManager.
Zend\Validator\ValidatorChain::getPluginManager()
initializes Zend\Validator\ValidatorPluginManager
on the fly if no plugins were set. This one does not go through the ServiceLocator and is created without a factory. Ergo it can not have any configurations given in its constructor.
Maybe I am wrong here, but this is the workflow it seems to follow, though I did not have the validators defined in the entity but in a dedicated InputFilter class.
I have same problem.
Resolve:
Zend\InputFilter\InputFilter
every time is creating a new instance of Zend\Validator\ValidatorPluginManager
because of this config from validators
don't loaded. For fix this need to inject one class manualy to Zend\InputFilter\InputFilter
.
Example:
// in a factory `InputFilterFactory`
$vpm = $container->get('PluginManager');
$if = new \Zend\InputFilter\InputFilter;
$if->getFactory()->getDefaultValidatorChain()->setPluginManager($vpm);
return $if;
Solution
Add Zend\InputFilter
to config/module.config.php
Configure validator and form element in module.config.php
'validators' => [
'factories' => [
CustomValidator::class => CustomValidatorFactory::class,
],
],
'form_elements' => [
'factories' => [
CustomElement::class => CustomElementFactory::class,
],
],
Set InputFilterPluginManager
into your CustomForm
form
class CustomFormFactory implements FactoryInterface
{
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
): CustomForm {
$form = new CustomForm($em);
$form->getFormFactory()
->getInputFilterFactory()
->setInputFilterManager(
$container->get(InputFilterPluginManager::class)
);
return $form;
}
}
Form
class CustomForm extends Form implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('custom-form');
$this->add(
[
'name' => 'submit',
'type' => Submit::class,
]
);
}
public function init()
{
$this->add([
'name' => 'name',
'type' => CustomElement::class,
]);
}
public function getInputFilterSpecification(): array
{
return [
'name' => [
'required' => true,
'validators' => [
['name' => CustomValidator::class],
],
],
];
}
}
Getting form
class CustomService
{
/**
* Injection
* @var ServiceManager
*/
$serviceManager;
public function getCustomForm(): CustomForm
{
if ($this->serviceManager->has(CustomForm::class)) {
return $this->serviceManager->get(CustomForm::class);
}
$form = $this->serviceManager->get('FormElementManager')->get(CustomForm::class);
$this->serviceManager->setService(CustomForm::class, $form);
return $form;
}
}
In the future, our forums are a better place to post problems such as this. That way, other developers can help you find a solution, or determine that a bug exists, and help you provide a reproduce case for the issue.
Hi,
It seems using annotations is not possible to use custom validators defined through a factory.
And no matter where I place the factory into module_config it doesnt get called...
The error is:
Zend\Validator\ValidatorPluginManager::get was unable to fetch or create an instance for uniqueobject