Note that there are some explanatory texts on larger screens.

plurals
  1. POZend_Form Captcha not validating
    primarykey
    data
    text
    <p>I've read quite some posts about this, but I havn't been able to solve my problem. When I try to validate my zend form captcha it always fails even with the correct text. Here is my code:</p> <p>// where i call my form</p> <pre><code> public function contactAction() { $this-&gt;view-&gt;form = new Forms_ContactForm(); } </code></pre> <p>//my form</p> <pre><code>class Forms_ContactForm extends Twitter_Form { public function init() { $this-&gt;setAction( 'email/email/contact' ); $this-&gt;setMethod('post'); $this-&gt;addElement('text', 'strFirstName', array( 'required' =&gt; true, 'label' =&gt; 'Your First Name' ) ); $this-&gt;addElement('text', 'strLastName', array( 'required' =&gt; true, 'label' =&gt; 'your Last Name' ) ); $this-&gt;addElement('text', 'strEmail', array( 'validators' =&gt; array( array('EmailAddress') ), 'required' =&gt; true, 'label' =&gt; 'Your Email' ) ); $this-&gt;addElement('textarea', 'strContent', array( 'required' =&gt; true, 'label' =&gt; 'Your message' ) ); $this-&gt;addElement('captcha', 'captcha', array( 'label' =&gt; 'Please enter the 5 letters displayed below:', 'required' =&gt; true, 'name' =&gt; 'captchaField', 'captcha' =&gt; 'image', 'captchaOptions' =&gt; array( 'captcha' =&gt; 'image', 'font'=&gt; 'static/font/arial.ttf', 'imgDir'=&gt;'static/img/captcha', 'imgUrl'=&gt; 'static/img/captcha/', 'wordLen' =&gt; 5, 'fsize'=&gt;20, 'height'=&gt;60, 'width'=&gt;200, 'gcFreq'=&gt;50, 'expiration' =&gt; 300) )); $this-&gt;addElement('submit', 'submit', array('class' =&gt; 'Submit') ); } } </code></pre> <p>// and my action to send it</p> <pre><code>public function contactAction() { if( $this-&gt;_request-&gt;isPost() ) { $objForm = new Forms_ContactForm(); if( $objForm-&gt;isValid($_POST) ) { $arrParams = $this-&gt;_request-&gt;getParams(); if( $arrParams['strFirstName'] &amp;&amp; $arrParams['strLastName'] &amp;&amp; $arrParams['strEmail'] &amp;&amp; $arrParams['strContent'] ) { $this-&gt;_objEmail = new Zend_Mail(); $this-&gt;_objEmail -&gt;setFrom( $arrParams['strEmail'] ); $this-&gt;_objEmail -&gt;setSubject( 'C' ); $this-&gt;_objEmail -&gt;setBodyHtml( 'Message from: '. $arrParams['strFirstName'] . ' ' . $arrParams['strLastName'] . '&lt;BR&gt;eMail address: ' . $arrParams['strEmail'] . '&lt;BR&gt;&lt;BR&gt;' . $arrParams['strContent'] ); $this-&gt;_objEmail -&gt;addTo( 'mail@gmail.com' ); $this-&gt;view-&gt;bolSent = $this-&gt;_objEmail-&gt;send(); } } else $this-&gt;view-&gt;form = $objForm; } } </code></pre> <p>It seems that in my contactAction, it generates a new captcha code, so that's why it soesnt match with the one I submitted, but I have no idea how to fix it.</p> <p>Thank you for your time and help !!</p> <p>Just saw something dodgy : when i dump my $_POST in contact action here is my result :</p> <pre><code>array 'strFirstName' =&gt; string 'fghjfghj' (length=8) 'strLastName' =&gt; string 'ffffffff' (length=8) 'strEmail' =&gt; string 'fvhkbno@biu.fr' (length=14) 'strContent' =&gt; string 'fewfew' (length=6) 'captchaField' =&gt; string 'cebfe69ead38dba86a6b557dc8853b24' (length=32) </code></pre> <p>The captcha I just entered dosent even appear and instead I have the captcha kay !!??</p> <p><strong>EDIT</strong></p> <p>thanks again for your replay !!! Still not there even with your changes but I think I got whats wrong. Here is my html for the captcha field:</p> <pre><code>&lt;div class="control-group"&gt; &lt;label class="control-label required" for="captchaField-input"&gt;Please enter the 5 letters displayed below:&lt;/label&gt; &lt;div class="controls"&gt; &lt;img width="200" height="60" src="static/img/captcha/ab2d15044a637338064b39cfd2675837.png" alt=""&gt; &lt;input type="hidden" id="captchaField-id" value="ab2d15044a637338064b39cfd2675837" name="captchaField[id]"&gt; &lt;input type="text" value="" id="captchaField-input" name="captchaField[input]"&gt; &lt;input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField"&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>When I have a look at my params sent, here is what I got :</p> <p>captchaField ab2d15044a637338064b39cfd2675837 captchaField[id] ab2d15044a637338064b39cfd2675837 captchaField[input] 6af7u</p> <p>It seems that cptchaField overwright [id] and [input]</p> <p>I feel like I need to remove this captchaField but have no idea how so far !</p> <p>I could do that with JS but there must be a clean way to do so !</p> <p><strong>EDIT AGAIN</strong></p> <p>Im using ajax to submit the form, with serialize. That could be the problem, ill have a look. </p> <p><strong>EDIT TER</strong></p> <p>It is not caused by ajax. If I manually remove the line :</p> <pre><code> &lt;input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField"&gt; </code></pre> <p>with firebug, everything is normal and the captcha validates well. Now the question is how to remove this line properly...</p> <p><strong>SOLUTION</strong></p> <p>After struggling a lot, here is the solution (remove decorator) !</p> <pre><code>$captcha = new Zend_Form_Element_Captcha('captchaField', array('label' =&gt; "Please enter the 5 letters displayed below:", 'required'=&gt;true, 'captcha' =&gt; array( 'captcha' =&gt; 'image', 'font'=&gt; 'static/font/arial.ttf', 'imgDir'=&gt;'static/img/captcha', 'imgUrl'=&gt; 'static/img/captcha/', 'wordLen' =&gt; 5, 'fsize'=&gt;20, 'height'=&gt;60, 'width'=&gt;200, 'gcFreq'=&gt;50, 'expiration' =&gt; 300 ) )); $this-&gt;addElement($captcha); $this-&gt;getElement('captchaField')-&gt;removeDecorator("viewhelper"); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload