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_Interface
25 require_once 'Zend/Validate/Interface.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
implements Zend_Validate_Interface
40 protected $_validators = array();
43 * Array of validation failure messages
47 protected $_messages = array();
54 protected static $_defaultNamespaces = array();
57 * Array of validation failure message codes
60 * @deprecated Since 1.5.0
62 protected $_errors = array();
65 * Adds a validator to the end of the chain
67 * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
68 * if one exists, will not be executed.
70 * @param Zend_Validate_Interface $validator
71 * @param boolean $breakChainOnFailure
72 * @return Zend_Validate Provides a fluent interface
74 public function addValidator(Zend_Validate_Interface
$validator, $breakChainOnFailure = false)
76 $this->_validators
[] = array(
77 'instance' => $validator,
78 'breakChainOnFailure' => (boolean
) $breakChainOnFailure
84 * Returns true if and only if $value passes all validations in the chain
86 * Validators are run in the order in which they were added to the chain (FIFO).
91 public function isValid($value)
93 $this->_messages
= array();
94 $this->_errors
= array();
96 foreach ($this->_validators
as $element) {
97 $validator = $element['instance'];
98 if ($validator->isValid($value)) {
102 $messages = $validator->getMessages();
103 $this->_messages
= array_merge($this->_messages
, $messages);
104 $this->_errors
= array_merge($this->_errors
, array_keys($messages));
105 if ($element['breakChainOnFailure']) {
113 * Defined by Zend_Validate_Interface
115 * Returns array of validation failure messages
119 public function getMessages()
121 return $this->_messages
;
125 * Defined by Zend_Validate_Interface
127 * Returns array of validation failure message codes
130 * @deprecated Since 1.5.0
132 public function getErrors()
134 return $this->_errors
;
138 * Returns the set default namespaces
142 public static function getDefaultNamespaces()
144 return self
::$_defaultNamespaces;
148 * Sets new default namespaces
150 * @param array|string $namespace
153 public static function setDefaultNamespaces($namespace)
155 if (!is_array($namespace)) {
156 $namespace = array((string) $namespace);
159 self
::$_defaultNamespaces = $namespace;
163 * Adds a new default namespace
165 * @param array|string $namespace
168 public static function addDefaultNamespaces($namespace)
170 if (!is_array($namespace)) {
171 $namespace = array((string) $namespace);
174 self
::$_defaultNamespaces = array_unique(array_merge(self
::$_defaultNamespaces, $namespace));
178 * Returns true when defaultNamespaces are set
182 public static function hasDefaultNamespaces()
184 return (!empty(self
::$_defaultNamespaces));
188 * @param mixed $value
189 * @param string $classBaseName
190 * @param array $args OPTIONAL
191 * @param mixed $namespaces OPTIONAL
193 * @throws Zend_Validate_Exception
195 public static function is($value, $classBaseName, array $args = array(), $namespaces = array())
197 $namespaces = array_merge((array) $namespaces, self
::$_defaultNamespaces, array('Zend_Validate'));
198 $className = ucfirst($classBaseName);
200 if (!class_exists($className, false)) {
201 require_once 'Zend/Loader.php';
202 foreach($namespaces as $namespace) {
203 $class = $namespace . '_' . $className;
204 $file = str_replace('_', DIRECTORY_SEPARATOR
, $class) . '.php';
205 if (Zend_Loader
::isReadable($file)) {
206 Zend_Loader
::loadClass($class);
213 $class = new ReflectionClass($className);
214 if ($class->implementsInterface('Zend_Validate_Interface')) {
215 if ($class->hasMethod('__construct')) {
216 $keys = array_keys($args);
218 foreach($keys as $key) {
219 if (is_numeric($key)) {
226 $object = $class->newInstanceArgs($args);
228 $object = $class->newInstance($args);
231 $object = $class->newInstance();
234 return $object->isValid($value);
236 } catch (Zend_Validate_Exception
$ze) {
237 // if there is an exception while validating throw it
239 } catch (Exception
$e) {
240 // fallthrough and continue for missing validation classes
243 require_once 'Zend/Validate/Exception.php';
244 throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
248 * Returns the maximum allowed message length
252 public static function getMessageLength()
254 require_once 'Zend/Validate/Abstract.php';
255 return Zend_Validate_Abstract
::getMessageLength();
259 * Sets the maximum allowed message length
261 * @param integer $length
263 public static function setMessageLength($length = -1)
265 require_once 'Zend/Validate/Abstract.php';
266 Zend_Validate_Abstract
::setMessageLength($length);