0

Was wondering if anyone has come across this problem previously. I am using the preconfigured form spec to create form using the Zend\Form\Factory, I am also injecting the FormElementManager into the factory so it can find my custom elements etc.

My question is:

Even tho any custom validators are registered with the form, they do not trigger isValid() method. Is there anything I need to do to get the isValid() triggered with creating the form with factory.

My source looks like following:

$spec = [
    'hydrator' => '...',
    'fieldset' => [
        ...,
        ...,
        ...,
    ],
    'input_filter' => [
        ...,
        ....
        ....,
        //contains my custom validator in here
    ],
];



$factory = new Factory();
$factory->setFormElementManager($formElementManager);
$form = $factory->createForm($spec);

But when I trigger:

$form->isValid()

It doesn't get to the isValid call in my custom validator.

1 Answers1

0

The input filter factory, Zend\InputFilter\Factory, is another dependency of the form factory. This factory is used by the form factory to create the inputs that should be filtered and validated.

In order to create the new inputs, and attach the custom filters and validators, the input factory uses the Zend\InputFilter\InputFilterPluginManager, which inside also seeds the two other plugin managers, the FilterManager and the ValidatorManager.

The Zend\Validator\ValidatorPluginManager is the plugin manager that would create your custom validator.

So by updating your code and manually setting this dependency would then allow the custom validators to be found via the Zend\InputFilter\Factory.

$formElementManager = $serviceManager->get('FormElementManager');
$inputFilterManager = $serviceManager->get('InputFilterManager');

$inputFilterFactory = new Zend\InputFilter\Factory();
$inputFilterFactory->setInputFilterManager($inputFilterManager);

$formFactory = new \Zend\Form\Factory();

$formFactory->setFormElementManager($formElementManager);
$formFactory->setInputFilterFactory($inputFilterFactory);

$form = $formFactory->createForm($spec);
AlexP
  • 9,906
  • 1
  • 24
  • 43
  • Thanks Alex, for your comment. – Bikash Poudel Jul 29 '16 at 15:43
  • The problem was not just that. Basically any zf2 form elements that is not required will not invoke the validators in the validator chain, which results in the issue i was facing. My solution was to basically add Not_Empty validator when the input was not required, which triggered all the validators including my custom validator. See: https://akrabat.com/category/zend-framework-2/ – Bikash Poudel Jul 29 '16 at 15:48