1

On my bootstrap I don't have a class, it's a simple php file:

I have added there:

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

Then, in application/validators I have:

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;  

    }  
}

In my form constructor I have:

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

And finally on my view I have:

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

Despite all this, I receive my form data, either by filling or not filling that text field. What am I missing here ?

Thanks a lot in advance.

MEM
  • 30,529
  • 42
  • 121
  • 191

2 Answers2

1

Thats expected behaviour. $honeypot is a form-element. Now, let's say you have a form $hp_form where $honeypot is one of the elements assigned.

Now, in your controller simply use something like:

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

Probably you also want to check, if you display the form for the first time or if the user submitted the form:

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

...assuming that your submit button has the id 'submit_button'.

Hope this helps

Bye, Christian

itsame69
  • 1,540
  • 5
  • 17
  • 37
0

replace :

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

by :

if (strlen($value) > 0)
{
   return true;
}
Ben
  • 305
  • 2
  • 6
  • @Ben: That will do the same no? Only shortening ? Either way, I change it and the issue remains. – MEM Jun 22 '11 at 17:23
  • you have your answer : change 'Spam' by 'My_Validate_Spam' into array validator – Ben Jun 22 '11 at 18:06
  • @Ben: I've update my question. Care to have a look ? It must be something related with the way I'm renaming files and place them on the application... – MEM Jun 22 '11 at 18:07
  • @Ben: I have changed Spam by My_Validate_Spam but I get the same error. But this time, telling the same with "My_Validate_Spam" :( - It should be related with the way Zend registers this, and perhaps, I must put the file Spam.php that extends the validation abstract class, somewhere else... – MEM Jun 22 '11 at 18:11
  • @Ben: I will create another question for dealing with this new issue. – MEM Jun 22 '11 at 18:31
  • http://stackoverflow.com/questions/6444879/zend-form-custom-validation-path-issues – MEM Jun 22 '11 at 18:49
  • i think i've found a solution, remove last underscore into your bootstrap, like : $resourceLoader->addResourceType('validate', 'validators/', 'My_Validate'); then test it – Ben Jun 22 '11 at 20:48