sfCaptchaGDValidator.class.php 1.34 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
class sfCaptchaGDValidator extends sfValidatorString
{
  /**
   * Configures the current validator.
   *
   * Available options:
   *
   *  * length: The Length of the string
   *
   * Available error codes:
   *
   *  * length
   *
   * @param array $options   An array of options
   * @param array $messages  An array of error messages
   *
   * @see sfValidatorBase
   */
  
  public function configure($options = array(), $messages = array())
  {
    $this->addMessage('length', '"%value%" must be %length% characters long.');
    
    $this->addMessage('invalid', 'The numbers you typed in are invalid.');

    $this->addOption('length');

    $this->setOption('empty_value', '');
  }
  
  protected function doClean($value)
  { 
    $clean = (string) $value;
    
    $length = function_exists('mb_strlen') ? mb_strlen($clean, $this->getCharset()) : strlen($clean);

    if ($this->hasOption('length') && $length != $this->getOption('length'))
    {
      throw new sfValidatorError($this, 'length', array('value' => $value, 'length' => $this->getOption('length')));
    }
        
    if (sfContext::getInstance()->getUser()->getAttribute('captcha', NULL) != $clean)
    {
      sfContext::getInstance()->getUser()->setAttribute('captcha', NULL);
      throw new sfValidatorError($this, 'invalid', array('value' => $value));
    }

    return $clean;
  }
}
?>