phpvalidationzend-framework2captchainput-filter

ZF2 Captcha validation ignored when using an input filter


I have a form in my ZF2 app with a CAPTCHA element as follows :

$this->add(array(
        'type' => 'Zend\Form\Element\Captcha',
        'name' => 'captcha',
        'attributes' => array(
            'class'=>'form-control',
        ),
        'options' => array(
            'label' => 'Please verify you are human.',
            'captcha' => array('class' => 'Dumb')
        ),
    ));

I have an input filter attached to the form that validates the other elements in the form (name, email, message). When this is attached to the form the validation for the CAPTCHA field is ignored when checking if valid.

if ($request->isPost()) {          
        // set the filter
        $form->setInputFilter($form->getInputFilter());
        $form->setData($request->getPost());
        if ($form->isValid()) { ...

If i remove the input filter then the CAPTCHA field is validated correctly but obviously the other fields have no validators. What silly mistake am I making? Is there a "CAPTCHA" validator I have to set in the input filter?


Solution

  • The issue is because, I assume that on your form you have created a method called:

    getInputFilter();

    which overrides the original getInputFilter(),

    there are two solutions:

    rename your function on your form to be getInputFilterCustom()

    and then modify also:

    if ($request->isPost()) {          
        // set the filter
        $form->setInputFilter($form->getInputFilterCustom());
    

    or inside your current getInputFilter() add the logic to validate the captcha.