zf2 captcha validation does not work on resend via refresh button

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

zf2 captcha validation does not work on resend via refresh button

narimani


I am using ZF2 v2.3.3 captcha in a form with this code: in indexController I have:

public function loginAction() {
        $loginForm = new LoginForm ( );
        if ($this->getRequest ()->isPost ()) {
            $postedValues = $this->getRequest ()->getPost ();
            $loginForm->setData ( $postedValues );
            if ($loginForm->isValid ()) {
                echo 'valid';
            } else {
                echo 'invalid';
            }
        }
    }
    $view ['loginForm'] = $loginForm;
    return new ViewModel ( $view );
}

in my loginForm I have :

public function __construct($adapter) {
    parent::__construct ();
    $this->setAttribute ( 'method', 'post' );
    $this->setAttribute ( 'name', 'loginForm' );
    $inputFilter = new InputFilter ();
    $factory = new Factory ();
    $captchaImage = new CaptchaImage(  array(
            'font' => dirname(__DIR__).'/../../../../public/fonts/arial.ttf',
            'width' => 150,
            'height' => 75,
            'dotNoiseLevel' => 40,
            'lineNoiseLevel' => 3 ,
            'wordLen' => 5
        )
    );
    $this->add(array(
        'type' => 'Zend\Form\Element\Captcha',
        'name' => 'captcha',
        'options' => array(
            'label' => 'Security Question',
            'captcha' => $captchaImage,
        ),
    ));
    $submit = new Element\Submit ( 'submit' );
    $submit->setValue ( _ ( 'Login' ) )->setAttributes ( array (
            'id' => 'submitbutton',
            'class' => 'btn btn-default'
    ) );
    $this->add ( $submit );
    }

and in my phtml file :

if(isset($this->loginForm)){    
    $form = $this->loginForm;
    $form->prepare();
    var_dump($form->getMessages());
echo $this->form()->openTag($form);
    ?>
<div class="col-md-6">
        <?php
        foreach ($form as $element) {
            echo '<div class="form-group">
                <label class="control-label">
                    '.
                    _($element->getLabel()).
                '</label>'
                    .$this->formElement($element).
                    '
                </div>';
        }
        ?>
</div>
<?php
    echo $this->form()->closeTag();
}

My problem is that when I submit the form everything works perfect. The isValid function does what it has to and validates captcha correctly. But when I resend the same info via my browser refresh button, the isValid function returns true again which is not acceptable because the image has changed. Am I missing something here?