Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * sfValidatorString validates a string. It also converts the input value to a string.
13
 *
14
 * @package    symfony
15
 * @subpackage validator
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfValidatorString.class.php 12641 2008-11-04 18:22:00Z fabien $
18
 */
19
class sfValidatorString extends sfValidatorBase
20
{
21
  /**
22
   * Configures the current validator.
23
   *
24
   * Available options:
25
   *
26
   *  * max_length: The maximum length of the string
27
   *  * min_length: The minimum length of the string
28
   *
29
   * Available error codes:
30
   *
31
   *  * max_length
32
   *  * min_length
33
   *
34
   * @param array $options   An array of options
35
   * @param array $messages  An array of error messages
36
   *
37
   * @see sfValidatorBase
38
   */
39
  protected function configure($options = array(), $messages = array())
40
  {
41
    $this->addMessage('max_length', '"%value%" is too long (%max_length% characters max).');
42
    $this->addMessage('min_length', '"%value%" is too short (%min_length% characters min).');
43
 
44
    $this->addOption('max_length');
45
    $this->addOption('min_length');
46
 
47
    $this->setOption('empty_value', '');
48
  }
49
 
50
  /**
51
   * @see sfValidatorBase
52
   */
53
  protected function doClean($value)
54
  {
55
    $clean = (string) $value;
56
 
57
    $length = function_exists('mb_strlen') ? mb_strlen($clean, $this->getCharset()) : strlen($clean);
58
 
59
    if ($this->hasOption('max_length') && $length > $this->getOption('max_length'))
60
    {
61
      throw new sfValidatorError($this, 'max_length', array('value' => $value, 'max_length' => $this->getOption('max_length')));
62
    }
63
 
64
    if ($this->hasOption('min_length') && $length < $this->getOption('min_length'))
65
    {
66
      throw new sfValidatorError($this, 'min_length', array('value' => $value, 'min_length' => $this->getOption('min_length')));
67
    }
68
 
69
    return $clean;
70
  }
71
}