2011-12-20 49 views
5

我需要使用Zend的驗證碼,並寫了follwoing代碼:如何在Zend Form中添加Captcha?

<?php 

    require_once ('Zend/Form.php'); 

    class Form_Signup extends Zend_Form { 

     public function __construct($options = null) { 

      parent::__construct($options); 

      // Set method 
      $this->setMethod('post'); 

      // Elements array 
      $elements = array(); 

      $element = new Zend_Form_Element_Captcha('captcha', array('label' => "", 
                     'captcha' => array('captcha' => 'Image', 
                         'name' => 'myCaptcha', 
                         'wordLen' => 5, 
                         'timeout' => 300, 
                         'font' => 'font/monofont.ttf', 
                         'imgDir' => 'captcha/', 
                         'imgUrl' => '/captcha/') 
                    ) 
                ); 
      $elements[] = $element; 

      // Submit Button 
      $element = $this->CreateElement('submit', 'Signup'); 
      $element->setLabel('Signup'); 
      $elements[] = $element; 

      // -------------------------- 
      // Add elements to the form 
      // -------------------------- 

      // update tabindex 
      foreach ($elements as $index => $element) { 
       $element->setAttrib('tabindex', ($index + 1)); 
      } 

      $this->addElements($elements); 
      $this->setElementDecorators(array('ViewHelper')); 

      // Set form decorator (what script will render the form) 
      $this->setDecorators(array(array('ViewScript' , array('viewScript' => 'signup/form.phtml')))); 

     } 

    } 

?> 

我遇到的問題是,當我喜歡我的「form.phtml」的文件顯示它:

<?= $this->element->captcha ?> 

它顯示以下內容:

enter image description here

即它顯示2個文本框。

請幫助我解決這種情況。 :)

+0

發佈它產生的標記。 – Dunhamzzz 2011-12-20 13:57:35

+1

您是否使用了完整的Zend_Form,或者只是將驗證碼輸出? – 2011-12-20 17:01:17

+0

我正在使用完整的Zend_Form。 – Ahsan 2011-12-21 07:16:18

回答

5

您正在獲取第二個文本字段,因爲您正在爲驗證碼元素使用ViewHelper裝飾器。不要爲驗證碼元素設置ViewHelper裝飾器,它應該可以工作。

0

你好我使用此代碼刪除多餘的場

$captcha = new Zend_Form_Element_Captcha('captcha',array('label' => 'Write the chars to the field', 
                  'captcha' => array(
                   'captcha' => 'Image', 
                   'wordLen' => 3, 
                   'timeout' => 300, 
                   'font' => APPLICATION_PATH.'/../public/font/two.ttf', 
                   'imgDir' => APPLICATION_PATH.'/../public/captcha/', 
                   'imgUrl' => 'http://localhost/projx/public/captcha/' 

                  ), 
              'decorators' => $this->elementDecorators, 
              )); 
              $captcha->removeDecorator('ViewHelper'); 

這是我的裝飾

'decorators' => $this->elementDecorators, 

這是默認的裝飾

我只是用ViewHelpre followig代碼

刪除
$captcha->removeDecorator('ViewHelper'); 

我希望這會對你有用...... :)