Unable to use custom validators when creating form using factory

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to use custom validators when creating form using factory

Bikash Poudel
Hello,

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:

<?php
$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.

Thank you,
Bikash
Reply | Threaded
Open this post in threaded view
|

Re: Unable to use custom validators when creating form using factory

Daniel
Hey!

In order to use custom validators, first you should register your custom
validator in ValidatorManager:

$validatorManager = new ValidatorManager();
$validatorManager->setService('custom', new MyCustomValidator());

Then register your ValidatorManager in InputFilter factory:

$formFactory = new Factory();
$formFactory->getInputFilterFactory()->getDefaultValidatorChain()->setPluginManager($validatorManager);

Now it should work :)

Em qui, 28 de jul de 2016 às 04:40, Bikash Poudel <
[hidden email]> escreveu:

> Hello,
>
> 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:
>
> <?php
> $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.
>
> Thank you,
> Bikash
>
--

Daniel Gimenes
twitter.com/danizord
github.com/danizord
Reply | Threaded
Open this post in threaded view
|

Re: Unable to use custom validators when creating form using factory

Bikash Poudel
Hey Daniel,

Thanks for the email.

My problem was not just that, and I dont think with the newer versions of
the framework you've got to get that far.
My issue was the validator was getting registered but was never triggering
isValid method. This was because the input for which the validator was
registered was a not required input and the validator chain gets terminated
as soon as the input is empty and is not required, and hence never
triggering your isValid; which i think is a odd behavior, when you are
dealing with the forms that contain complicated logic of show/hide fields
depending upon other fields.

I managed to solve this using continue_if_empty option.

Thanks,
Bikash

On 31 July 2016 at 18:12, Daniel Gimenes <[hidden email]>
wrote:

> Hey!
>
> In order to use custom validators, first you should register your custom
> validator in ValidatorManager:
>
> $validatorManager = new ValidatorManager();
> $validatorManager->setService('custom', new MyCustomValidator());
>
> Then register your ValidatorManager in InputFilter factory:
>
> $formFactory = new Factory();
>
> $formFactory->getInputFilterFactory()->getDefaultValidatorChain()->setPluginManager($validatorManager);
>
> Now it should work :)
>
> Em qui, 28 de jul de 2016 às 04:40, Bikash Poudel <
> [hidden email]> escreveu:
>
>> Hello,
>>
>> 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:
>>
>> <?php
>> $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.
>>
>> Thank you,
>> Bikash
>>
> --
>
> Daniel Gimenes
> twitter.com/danizord
> github.com/danizord
>