7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Validate
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * @see Zend_Validate_Abstract
25 require_once 'Zend/Validate/Abstract.php';
29 * @package Zend_Validate
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
33 class Zend_Validate_Alnum
extends Zend_Validate_Abstract
35 const INVALID
= 'alnumInvalid';
36 const NOT_ALNUM
= 'notAlnum';
37 const STRING_EMPTY
= 'alnumStringEmpty';
40 * Whether to allow white space characters; off by default
45 public $allowWhiteSpace;
48 * Alphanumeric filter used for validation
50 * @var Zend_Filter_Alnum
52 protected static $_filter = null;
55 * Validation failure message template definitions
59 protected $_messageTemplates = array(
60 self
::INVALID
=> "Invalid type given, value should be float, string, or integer",
61 self
::NOT_ALNUM
=> "'%value%' contains characters which are non alphabetic and no digits",
62 self
::STRING_EMPTY
=> "'%value%' is an empty string",
66 * Sets default option values for this instance
68 * @param boolean|Zend_Config $allowWhiteSpace
71 public function __construct($allowWhiteSpace = false)
73 if ($allowWhiteSpace instanceof Zend_Config
) {
74 $allowWhiteSpace = $allowWhiteSpace->toArray();
77 if (is_array($allowWhiteSpace)) {
78 if (array_key_exists('allowWhiteSpace', $allowWhiteSpace)) {
79 $allowWhiteSpace = $allowWhiteSpace['allowWhiteSpace'];
81 $allowWhiteSpace = false;
85 $this->allowWhiteSpace
= (boolean
) $allowWhiteSpace;
89 * Returns the allowWhiteSpace option
93 public function getAllowWhiteSpace()
95 return $this->allowWhiteSpace
;
99 * Sets the allowWhiteSpace option
101 * @param boolean $allowWhiteSpace
102 * @return Zend_Filter_Alnum Provides a fluent interface
104 public function setAllowWhiteSpace($allowWhiteSpace)
106 $this->allowWhiteSpace
= (boolean
) $allowWhiteSpace;
111 * Defined by Zend_Validate_Interface
113 * Returns true if and only if $value contains only alphabetic and digit characters
115 * @param string $value
118 public function isValid($value)
120 if (!is_string($value) && !is_int($value) && !is_float($value)) {
121 $this->_error(self
::INVALID
);
125 $this->_setValue($value);
128 $this->_error(self
::STRING_EMPTY
);
132 if (null === self
::$_filter) {
134 * @see Zend_Filter_Alnum
136 require_once 'Zend/Filter/Alnum.php';
137 self
::$_filter = new Zend_Filter_Alnum();
140 self
::$_filter->allowWhiteSpace
= $this->allowWhiteSpace
;
142 if ($value != self
::$_filter->filter($value)) {
143 $this->_error(self
::NOT_ALNUM
);