1

私のブートストラップにはクラスがありません。これは単純な php ファイルです。

そこに追加しました:

$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>

これらすべてにもかかわらず、そのテキストフィールドに入力するか入力しないかのいずれかで、フォームデータを受け取ります。ここで何が欠けていますか?

よろしくお願いします。

4

2 に答える 2

1

それは予想される動作です。$honeypot はフォーム要素です。ここで、$honeypot が割り当てられた要素の 1 つであるフォーム $hp_form があるとします。

ここで、コントローラーで次のようなものを使用するだけです。

 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」であると仮定します。

お役に立てれば

さようなら、クリスチャン

于 2011-06-22T17:12:25.360 に答える
0

交換 :

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

に :

if (strlen($value) > 0)
{
   return true;
}
于 2011-06-22T17:12:35.137 に答える