Source for file Validator.php
Documentation is available at Validator.php
* $Id: Validator.php 2193 2007-08-10 06:01:54Z nicobn $
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
* Doctrine_Validator performs validations in record properties
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @version $Revision: 2193 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @var array $validators an array of validator objects
private static $validators =
array();
* returns a validator object
* @return Doctrine_Validator_Interface
if ( ! isset
(self::$validators[$name])) {
$class =
'Doctrine_Validator_' .
ucwords(strtolower($name));
self::$validators[$name] =
new $class;
throw
new Doctrine_Exception("Validator named '$name' not available.");
return self::$validators[$name];
* validates a given record and saves possible errors
* in Doctrine_Validator::$stack
* @param Doctrine_Record $record
$columns =
$record->getTable()->getColumns();
$component =
$record->getTable()->getComponentName();
$errorStack =
$record->getErrorStack();
// if record is transient all fields will be validated
// if record is persistent only the modified fields will be validated
$data =
($record->exists()) ?
$record->getModified() :
$record->getData();
foreach ($data as $key =>
$value) {
if ($value ===
self::$_null) {
$value =
$value->getIncremented();
$column =
$columns[$key];
if ($column['type'] ==
'enum') {
$value =
$record->getTable()->enumIndex($key, $value);
$errorStack->add($key, 'enum');
if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) {
$errorStack->add($key, 'length');
foreach ($column as $name =>
$args) {
||
$name ==
'autoincrement'
||
$name ==
'zerofill') {
if (strtolower($name) ===
'notnull' && isset
($column['autoincrement'])) {
if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) {
$errorStack->add($key, 'length');
if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) {
if ( ! self::isValidType($value, $column['type'])) {
$errorStack->add($key, 'type');
$validator =
self::getValidator($name);
if ( ! $validator->validate($record, $key, $value, $args)) {
$errorStack->add($key, $name);
//$err[$key] = 'not valid';
// errors found quit validation looping for this column
if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) {
if ( ! self::isValidType($value, $column['type'])) {
$errorStack->add($key, 'type');
* Validates the length of a field.
if ($column['type'] ==
'timestamp' ||
$column['type'] ==
'integer') {
} elseif ($column['type'] ==
'array' ||
$column['type'] ==
'object') {
if ($length >
$column['length']) {
* whether or not this validator has errors
return (count($this->stack) >
0);
* converts a doctrine type to native php type
public static function phpType($doctrineType)
* returns whether or not the given variable is
if ($type ==
'boolean') {
$looseType =
self::gettype($var);
$type =
self::phpType($type);
if ($type ==
'string' ||
$type ==
'float') {
return ($type ===
$looseType);
* returns the type of loosely typed variable
public static function gettype($var)