-- GJ Bogaerts <[email protected]> wrote
(on Thursday, 25 June 2009, 01:19 PM -0700):
> I try to implement a custom captcha element: the user sees a number spelled
> out (ie 'twenty' and is supposed to give the numerical equivalent: 20). If
> he passes that test, he is supposed to be human.
You need to persist the number in a session key so that when the form is
submitted, you have the original value to compare against.
You might want to instead create a custom adapter for Zend_Captcha,
which has features already built in for this sort of thing.
> This is the code for my custom element:
> <?php
> class Elements_Randcap extends Zend_Form_Element_Text
> {
>
> private $numbers = array();
> private $numberToCheck;
>
> public function init ()
> {
>
> $this->numbers =
> array("nul","een","twee","drie","vier","vijf","zes");
> //much longer array needed
> $this->numberToCheck = floor(rand(0,count($this->numbers)-1));
> $captcha = $this->numbers[$this->numberToCheck];
> $this->setLabel('Typ dit getal in cijfers:<strong>' . $captcha .
> '</strong>');
>
> $this->addValidator('Randcap',false,array($this->numberToCheck));
> }
> }
> ?>
> This is my custom validator
>
> <?php
> class Validators_Randcap extends Zend_Validate_Abstract {
> const IS_BOT = 'NoBlog';
>
> protected $_messageTemplates = array(self::IS_BOT => 'Het antispam getal
> was niet juist ingevuld');
>
> /**
> * The integer that is passed by the form
> *
> * @var int
> */
> protected $_captcha;
>
> /**
> *
> * @param int numberToCheck
> */
> public function __construct($numberToCheck) {
> $this->_captcha = $numberToCheck;
> }
>
> /**
> * Check if the element using this validator is valid
> *
> *
> * @param $value string
> * @return boolean Returns true if the element is valid
> */
> public function isValid($value) {
> $value = (int) $value;
> $this->_setValue($value);
> $error = false;
> if ($this->_captcha != $value) {
> $error = true;
> $this->_error(self::IS_BOT);
> $errorMessage = 'Het antispam getal was niet juist ingevuld';
> $this->setMessage($errorMessage, self::IS_BOT);
> }
>
> return !$error;
> }
> }
> ?>
>
> However, this doesn't work. It seems as if the validator is called twice:
> once upon instantiating the form, the element generates a captcha number.
> And when I check the post in my controller (with $form->isValid), it seems
> as if another captcha number is evaluated.
>
> What should I do to solve this problem?
>
> Thanks for any help!
>
>
> --
> View this message in context:
> http://www.nabble.com/custom-form-captcha-problem-tp24210371p24210371.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
--
Matthew Weier O'Phinney
Project Lead | [email protected]
Zend Framework | http://framework.zend.com/