2012-06-16 5 views
4

J'ai lu quelques articles à ce sujet, mais je n'ai pas été capable de résoudre mon problème. Lorsque j'essaie de valider mon captcha zend form, il échoue toujours même avec le texte correct. Voici mon code:Zend_Form Captcha ne valide pas

// où j'appelle ma forme

public function contactAction() 
{ 
    $this->view->form = new Forms_ContactForm(); 
} 

// ma forme

class Forms_ContactForm extends Twitter_Form 
{ 
public function init() 
{ 
    $this->setAction('email/email/contact'); 
    $this->setMethod('post'); 

    $this->addElement('text', 'strFirstName', array( 
      'required' => true, 
      'label' => 'Your First Name')); 

    $this->addElement('text', 'strLastName', array( 
      'required' => true, 
      'label' => 'your Last Name')); 

    $this->addElement('text', 'strEmail', array('validators' => array(
      array('EmailAddress')), 
      'required' => true, 
      'label' => 'Your Email')); 

    $this->addElement('textarea', 'strContent', array( 
      'required' => true, 
      'label' => 'Your message')); 

    $this->addElement('captcha', 'captcha', array(
      'label'  => 'Please enter the 5 letters displayed below:', 
      'required' => true, 
          'name'  => 'captchaField', 
      'captcha' => 'image', 
      'captchaOptions' => array( 
          'captcha' => 'image', 
          'font'=> 'static/font/arial.ttf', 
          'imgDir'=>'static/img/captcha', 
          'imgUrl'=> 'static/img/captcha/', 
        'wordLen' => 5, 
        'fsize'=>20, 
        'height'=>60, 
        'width'=>200, 
        'gcFreq'=>50, 
        'expiration' => 300) 

      )); 


    $this->addElement('submit', 'submit', array('class' => 'Submit')); 
} 
} 

// et mon action pour l'envoyer

public function contactAction() 
{ 
    if($this->_request->isPost()) 
    { 
     $objForm = new Forms_ContactForm(); 

     if($objForm->isValid($_POST)) 
     { 
      $arrParams = $this->_request->getParams(); 
      if($arrParams['strFirstName'] && $arrParams['strLastName'] && $arrParams['strEmail'] && $arrParams['strContent']) 
      { 
       $this->_objEmail = new Zend_Mail(); 
       $this->_objEmail ->setFrom($arrParams['strEmail']); 
       $this->_objEmail ->setSubject('C'); 
       $this->_objEmail ->setBodyHtml('Message from: '. $arrParams['strFirstName'] . ' ' . $arrParams['strLastName'] . 
               '<BR>eMail address: ' . $arrParams['strEmail'] . '<BR><BR>' 
               . $arrParams['strContent']); 

       $this->_objEmail ->addTo('[email protected]'); 

       $this->view->bolSent = $this->_objEmail->send(); 
      } 
     } 
     else 
      $this->view->form = $objForm; 
    } 
} 

Il semble que dans mon contactAction, il génère un nouveau code captcha, c'est pourquoi il ne correspond pas avec celui que je submi mais je n'ai aucune idée de comment y remédier.

Nous vous remercions de votre temps et de votre aide !!

Je viens de voir quelque chose de louche: quand je largue mon _POST $ en action de contact ici est mon résultat:

array 
'strFirstName' => string 'fghjfghj' (length=8) 
'strLastName' => string 'ffffffff' (length=8) 
'strEmail' => string '[email protected]' (length=14) 
'strContent' => string 'fewfew' (length=6) 
'captchaField' => string 'cebfe69ead38dba86a6b557dc8853b24' (length=32) 

Le captcha Je viens entré dosent même apparaître et à la place je le captcha kay !! ??

EDIT

merci encore pour votre replay !!! Toujours pas là, même avec vos changements, mais je pense que j'ai eu quoi de mal. Voici mon html pour le champ captcha:

<div class="control-group"> 
    <label class="control-label required" for="captchaField-input">Please enter the 5 letters displayed below:</label> 
    <div class="controls"> 
     <img width="200" height="60" src="static/img/captcha/ab2d15044a637338064b39cfd2675837.png" alt=""> 
     <input type="hidden" id="captchaField-id" value="ab2d15044a637338064b39cfd2675837" name="captchaField[id]"> 
     <input type="text" value="" id="captchaField-input" name="captchaField[input]"> 
     <input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField"> 
    </div> 
</div> 

Quand je regarde mes params envoyé, voici ce que je suis:

captchaField ab2d15044a637338064b39cfd2675837 captchaField [id] ab2d15044a637338064b39cfd2675837 captchaField [entrée] 6af7u

Il semble que cptchaField overwright [id] et [entrée]

Je sens que je dois enlever ce captchaField mais je ne sais pas comment jusqu'ici!

Je pourrais le faire avec JS mais il doit y avoir un moyen propre de le faire!

EDIT DE NOUVEAU

Im en utilisant ajax pour soumettre le formulaire, avec serialize. Cela pourrait être le problème, mal jeter un oeil.

EDIT TER

Il n'est pas causée par ajax. Si je supprimer manuellement la ligne:

<input type="text" value="ab2d15044a637338064b39cfd2675837" id="captchaField" name="captchaField"> 

avec Firebug, tout est normal et le captcha valide bien. Maintenant, la question est de savoir comment supprimer cette ligne correctement ...

SOLUTION

Après avoir lutté beaucoup, voici la solution (enlever décorateur)!

$captcha = new Zend_Form_Element_Captcha('captchaField', 
     array('label' => "Please enter the 5 letters displayed below:", 
      'required'=>true, 
      'captcha' => array(
      'captcha' => 'image', 
      'font'=> 'static/font/arial.ttf', 
      'imgDir'=>'static/img/captcha', 
      'imgUrl'=> 'static/img/captcha/', 
      'wordLen' => 5, 
      'fsize'=>20, 
      'height'=>60, 
      'width'=>200, 
      'gcFreq'=>50, 
      'expiration' => 300 
     ) 
    )); 

    $this->addElement($captcha);  
    $this->getElement('captchaField')->removeDecorator("viewhelper"); 
+0

comment est votre élément Captcha affiché dans votre fichier de vue? – Haroon

+0

Est-ce que le code dans le fichier de vue ou est-il la source HTML de la page? – Haroon

+0

mon code de vue est tout simplement echo $ this-> forme Ceci est le code source, construit automatiquement par zend – mokk

Répondre

4

Après avoir lutté un beaucoup, voici la solution (j'ai simplement dû enlever le décorateur)!

$captcha = new Zend_Form_Element_Captcha('captchaField', 
    array('label' => "Please enter the 5 letters displayed below:", 
     'required'=>true, 
     'captcha' => array(
     'captcha' => 'image', 
     'font'=> 'static/font/arial.ttf', 
     'imgDir'=>'static/img/captcha', 
     'imgUrl'=> 'static/img/captcha/', 
     'wordLen' => 5, 
     'fsize'=>20, 
     'height'=>60, 
     'width'=>200, 
     'gcFreq'=>50, 
     'expiration' => 300 
    ) 
)); 

$this->addElement($captcha);  
$this->getElement('captchaField')->removeDecorator("viewhelper"); 

Merci Haroon pour votre aide et votre temps :)

1

Je pense que vous code doit être comme ci-dessous, je pense que dans le fichier contactAction.php le public function contactAction() doit gérer l'affichage de la forme et l'action lorsque le formulaire a été affecté à):

//Display the form

$objForm = new Forms_ContactForm(); 

$this->view->form = $objForm; 



//Handle the form, when it has been posted including validation etc 

if($this->_request->isPost()) 
{ 
    if($objForm->isValid($_POST)) 
    { 
     //processing logic etc 
    }   
} 

Actuellement votre code génère un nouveau Captcha pour confirmer contre l'entrée de données, parce que vous instanciation la forme après que le formulaire a été affecté à. Cet instantian de la forme doit être fait avant que le formulaire a été posté, comme je l'ai montré dans l'exemple de code ci-dessus.

EDIT

Essayez ceci:

$captcha = new Zend_Form_Element_Captcha('captchaField', 
    array('label' => "Please enter the 5 letters displayed below:", 
     'required'=>true, 
     'captcha' => array(
     'captcha' => 'image', 
     'font'=> 'static/font/arial.ttf', 
     'imgDir'=>'static/img/captcha', 
     'imgUrl'=> 'static/img/captcha/', 
     'wordLen' => 5, 
     'fsize'=>20, 
     'height'=>60, 
     'width'=>200, 
     'gcFreq'=>50, 
     'expiration' => 300 
    ) 
)); 

$this->addElement($captcha); 

Ce nouveau code après le montage fonctionne pour moi d'utiliser la version Zend Framework 1.11.1

Quelle version de Zend Framework êtes vous chantez et comment est-ce que Captcha est affiché dans votre fichier de vue ??

Lorsque var le dumping notre votre sortie, pour le captcha elment vous devriez vous attendre quelque chose de semblable au-dessous où « 5g4ef » est les données entrées dans l'élément d'entrée Captcha:

["captchaField"]=> array(2) { ["id"]=> string(32) "88bb26d62e9fa19b67937c35be4a8cc7" ["input"]=> string(4) "5g4ef" }

+0

Merci pour votre réponse rapide. Malheureusement, même en changeant mon code comme vous l'avez dit, je suis toujours incapable de le valider. – mokk

+0

merci encore pour votre replay: – mokk

+0

@ mokk np - laissez-moi savoir si cela a fonctionné pour vous ou non. Nous voulons que les autres puissent savoir si/comment ce problème a été résolu. – Haroon