2011-06-22 38 views
1

在我的引導,我沒有一類,這是一個簡單的PHP文件:Zend的蜜罐驗證

我已經加入有:

$loader = Zend_Loader_Autoloader::getInstance(); 
$loader->setFallbackAutoloader (true); 
$loader->suppressNotFoundWarnings (false); 

//resource Loader 
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
       'basePath' => APPLICATION_PATH, 
       'namespace' => '', 
      )); 

$resourceLoader->addResourceType('validate', 'validators/', 'My_Validate_'); 

$loader->pushAutoloader($resourceLoader); 

然後,在應用程序/驗證我:

class My_Validate_Spam extends Zend_Validate_Abstract { 

    const SPAM = 'spam'; 

    protected $_messageTemplates = array( 
     self::SPAM => "Spammer" 
    ); 

    public function isValid($value, $context=null) 
    { 

     $value = (string)$value; 
     $this->_setValue($value); 

     if(is_string($value) and $value == ''){ 
      return true; 
     } 

     $this->_error(self::SPAM); 
     return false; 

    } 
} 

在我的窗體的構造我:

$this->addElement( 
       'text', 
       'honeypot', 
       array( 
        'label' => 'Honeypot', 
        'required' => false, 
        'class' => 'honeypot', 
        'decorators' => array('ViewHelper'), 
        'validators' => array( 
         array( 
          'validator' => 'Spam' 
         ) 
        ) 
       ) 
      ); 

最後對我的看法,我有:

<dt><label for="honeypot">Honeypot Test:</label></dt> 
<dd><?php echo $this->form->honeypot;?></dd> 

儘管這一切,我收到我的表單數據,無論是通過填充或不填充該文本字段。 我在這裏錯過了什麼?

非常感謝。

回答

1

這就是預期的行爲。 $ honeypot是一個表單元素。現在,假設您有一個$ hp_form表單,其中$ honeypot是分配的元素之一。現在

,在你的控制器只需使用類似:

if ($hp_form->isValid($this->getRequest()->getPost())) { 
    // do something meaningful with your data here 
} 

也許你還需要檢查,如果你是第一次或者如果用戶提交的表單顯示錶單:

if ($this->getRequest()->isPost() && 
     false !== $this->getRequest()->getPost('submit_button', false)) { 
    if ($hp_form->isValid($this->getRequest()->getPost())) { 
     // do something meaningful with your data here 
    } 
} 

...假設您的提交按鈕的ID爲'submit_button'。

希望這有助於

再見, 基督教

0

取代:

if (is_string($value) and $value == ''){ 
    return true; 
} 

由:

if (strlen($value) > 0) 
{ 
    return true; 
} 
+0

@Ben:那會做同樣的沒有?只有縮短?無論哪種方式,我改變它,問題依然存在。 – MEM

+0

你有你的答案:將'垃圾'改爲'My_Validate_Spam'進入陣列驗證器 – Ben

+0

@Ben:我已更新我的問題。小心一看?它必須是與我重命名文件並將它們放在應用程序上的方式有關... – MEM