~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/validators/CExistValidator.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CExistValidator class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 
8
 * @license http://www.yiiframework.com/license/
 
9
 */
 
10
 
 
11
/**
 
12
 * CExistValidator validates that the attribute value exists in a table.
 
13
 *
 
14
 * This validator is often used to verify that a foreign key contains a value
 
15
 * that can be found in the foreign table.
 
16
 *
 
17
 * @author Qiang Xue <qiang.xue@gmail.com>
 
18
 * @version $Id: CExistValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
19
 * @package system.validators
 
20
 * @since 1.0.4
 
21
 */
 
22
class CExistValidator extends CValidator
 
23
{
 
24
        /**
 
25
         * @var string the ActiveRecord class name that should be used to
 
26
         * look for the attribute value being validated. Defaults to null,
 
27
         * meaning using the ActiveRecord class of the attribute being validated.
 
28
         * You may use path alias to reference a class name here.
 
29
         * @see attributeName
 
30
         */
 
31
        public $className;
 
32
        /**
 
33
         * @var string the ActiveRecord class attribute name that should be
 
34
         * used to look for the attribute value being validated. Defaults to null,
 
35
         * meaning using the name of the attribute being validated.
 
36
         * @see className
 
37
         */
 
38
        public $attributeName;
 
39
        /**
 
40
         * @var array additional query criteria. This will be combined with the condition
 
41
         * that checks if the attribute value exists in the corresponding table column.
 
42
         * This array will be used to instantiate a {@link CDbCriteria} object.
 
43
         * @since 1.0.8
 
44
         */
 
45
        public $criteria=array();
 
46
        /**
 
47
         * @var boolean whether the attribute value can be null or empty. Defaults to true,
 
48
         * meaning that if the attribute is empty, it is considered valid.
 
49
         */
 
50
        public $allowEmpty=true;
 
51
 
 
52
        /**
 
53
         * Validates the attribute of the object.
 
54
         * If there is any error, the error message is added to the object.
 
55
         * @param CModel $object the object being validated
 
56
         * @param string $attribute the attribute being validated
 
57
         */
 
58
        protected function validateAttribute($object,$attribute)
 
59
        {
 
60
                $value=$object->$attribute;
 
61
                if($this->allowEmpty && $this->isEmpty($value))
 
62
                        return;
 
63
 
 
64
                $className=$this->className===null?get_class($object):Yii::import($this->className);
 
65
                $attributeName=$this->attributeName===null?$attribute:$this->attributeName;
 
66
                $finder=CActiveRecord::model($className);
 
67
                $table=$finder->getTableSchema();
 
68
                if(($column=$table->getColumn($attributeName))===null)
 
69
                        throw new CException(Yii::t('yii','Table "{table}" does not have a column named "{column}".',
 
70
                                array('{column}'=>$attributeName,'{table}'=>$table->name)));
 
71
 
 
72
                $criteria=array('condition'=>$column->rawName.'=:vp','params'=>array(':vp'=>$value));
 
73
                if($this->criteria!==array())
 
74
                {
 
75
                        $criteria=new CDbCriteria($criteria);
 
76
                        $criteria->mergeWith($this->criteria);
 
77
                }
 
78
 
 
79
                if(!$finder->exists($criteria))
 
80
                {
 
81
                        $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} "{value}" is invalid.');
 
82
                        $this->addError($object,$attribute,$message,array('{value}'=>$value));
 
83
                }
 
84
        }
 
85
}
 
86