Yii Framework v1.1.10 类参考

CCompareValidator

system.validators
继承 class CCompareValidator » CValidator » CComponent
源自 1.0
版本 $Id: CCompareValidator.php 3515 2011-12-28 12:29:24Z mdomba $
源码 framework/validators/CCompareValidator.php
CCompareValidator使用给定的属性值与另一个值进行比较,验证他们是否相等。

用于比较的值可以是另一个属性的值 (通过compareAttribute指定)或一个常量 (通过compareValue指定)。如果这两种情 形都指定了,那么后一个优先。如果没有指定任何情 形,将使用另一个属性的值进行比较,该属性的名字是 在需比较的属性的名字后加上“_repeat”组成。

比较的模式可以是严格比较(strict),也可以不是。

CCompareValidator现在支持多种比较操作符, 之前,它只是比较并检测两个值是否相等。

当使用message属性定义了一个自定义错误消息,此消息 可以包含附加的占位符,这些占位符将被实际内容替换。除了所有 验证器都可以识别的“{attribute}”占位符之外(参见CValidator), CCompareValidator还允许指定下列占位符:
  • {compareValue}: 用于与compareValue相比较的常量的值。

公共属性

隐藏继承属性

属性类型描述定义在
allowEmpty boolean 此属性值是否可以为null或empty。默认值为false。 如果设置为true,表示当它为空时此属性也会通过验证。 CCompareValidator
attributes array 需要被验证的属性的列表。 CValidator
builtInValidators array 内置验证器列表 (name=>class) CValidator
compareAttribute string 用于比较的属性的名字。 CCompareValidator
compareValue string 用于比较的常量值 CCompareValidator
enableClientValidation boolean 是否执行客户端验证。默认值为true。 参见CActiveForm::enableClientValidation以了解更多关于客户端验证的细节。 CValidator
message string 用户自定义的错误提示信息。不同的验证器可以在该信息中 定义各种占位符(将被实际值替换)。占位符“{attribute}”可以被所有 验证器识别,它会被使用属性的标签来替换。 CValidator
on array 验证器将被应用到的情景模式的列表。 数组的键-值都是情景模式的名称。 CValidator
operator string 用于比较的操作符。默认值是“=”。 下列操作符是有效的:
  • '=' 或 '==': 验证两个值是否相等。如果strict设置为true, 将会使用严格模式进行比较(即同时比较类型和值)。
  • '!=': 验证两个值是否不等。如果strict设置为true, 将会使用严格模式进行比较(即同时比较类型和值)。
  • '>': 验证需要检测的属性的值是否大于用来比较的值。
  • '>=': 验证需要检测的属性的值是否大于或等于用来比较的值。
  • '<': 验证需要检测的属性的值是否小于用来比较的值。
  • '<=': 验证需要检测的属性的值是否小于或等于用来比较的值。
CCompareValidator
safe boolean 进行整块赋值是是否考虑此验证器中列出的属性的安全性。 默认值为true。 CValidator
skipOnError boolean 如果当前属性已经存在验证错误,这个验证规则 是否跳过。默认值是false。 CValidator
strict boolean 是否执行严格的比较(值与类型都必须完全一致) 默认值为false。 CCompareValidator

公共方法

隐藏继承方法

方法描述定义在
__call() 如果类中没有调的方法名,则调用这个方法。 CComponent
__get() 返回一个属性值、一个事件处理程序列表或一个行为名称。 CComponent
__isset() 检查一个属性是否为null。 CComponent
__set() 设置一个组件的属性值。 CComponent
__unset() 设置一个组件的属性为null。 CComponent
applyTo() 返回值指定此验证器是否应用到指定的情景模式。 CValidator
asa() 返回这个名字的行为对象。 CComponent
attachBehavior() 附加一个行为到组件。 CComponent
attachBehaviors() 附加一个行为列表到组件。 CComponent
attachEventHandler() 为事件附加一个事件处理程序。 CComponent
canGetProperty() 确定属性是否可读。 CComponent
canSetProperty() 确定属性是否可写。 CComponent
clientValidateAttribute() 返回执行客户端验证所需的JavaScript。 CCompareValidator
createValidator() 创建一个验证器对象。 CValidator
detachBehavior() 从组件中分离一个行为。 CComponent
detachBehaviors() 从组件中分离所有行为。 CComponent
detachEventHandler() 分离一个存在的事件处理程序。 CComponent
disableBehavior() 禁用一个附加行为。 CComponent
disableBehaviors() 禁用组件附加的所有行为。 CComponent
enableBehavior() 启用一个附加行为。 CComponent
enableBehaviors() 启用组件附加的所有行为。 CComponent
evaluateExpression() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
getEventHandlers() 返回一个事件的附加处理程序列表。 CComponent
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasProperty() 确定属性是否被定义。 CComponent
raiseEvent() 发起一个事件。 CComponent
validate() 验证给定的对象。 CValidator

受保护方法

隐藏继承方法

方法描述定义在
addError() 添加关于指定属性的一个错误提示信息到活动记录中。 CValidator
isEmpty() 检测给定值是否为空。 CValidator
validateAttribute() 验证传入对象的属性。 CCompareValidator

属性详细

allowEmpty 属性
public boolean $allowEmpty;

此属性值是否可以为null或empty。默认值为false。 如果设置为true,表示当它为空时此属性也会通过验证。

compareAttribute 属性
public string $compareAttribute;

用于比较的属性的名字。

compareValue 属性
public string $compareValue;

用于比较的常量值

operator 属性
public string $operator;

用于比较的操作符。默认值是“=”。 下列操作符是有效的:

strict 属性
public boolean $strict;

是否执行严格的比较(值与类型都必须完全一致) 默认值为false。

方法详细

clientValidateAttribute() 方法 (可用自 v1.1.7)
public string clientValidateAttribute(CModel $object, string $attribute)
$object CModel 需要验证的数据对象
$attribute string 需要验证的属性名字
{return} string 客户端验证脚本
源码: framework/validators/CCompareValidator.php#153 (显示)
public function clientValidateAttribute($object,$attribute)
{
    if(
$this->compareValue !== null)
    {
        
$compareTo=$this->compareValue;
        
$compareValue=CJSON::encode($this->compareValue);
    }
    else
    {
        
$compareAttribute=$this->compareAttribute === null $attribute '_repeat' $this->compareAttribute;
        
$compareValue="\$('#" . (CHtml::activeId($object$compareAttribute)) . "').val()";
        
$compareTo=$object->getAttributeLabel($compareAttribute);
    }

    
$message=$this->message;
    switch(
$this->operator)
    {
        case 
'=':
        case 
'==':
            if(
$message===null)
                
$message=Yii::t('yii','{attribute} must be repeated exactly.');
            
$condition='value!='.$compareValue;
            break;
        case 
'!=':
            if(
$message===null)
                
$message=Yii::t('yii','{attribute} must not be equal to "{compareValue}".');
            
$condition='value=='.$compareValue;
            break;
        case 
'>':
            if(
$message===null)
                
$message=Yii::t('yii','{attribute} must be greater than "{compareValue}".');
            
$condition='parseFloat(value)<=parseFloat('.$compareValue.')';
            break;
        case 
'>=':
            if(
$message===null)
                
$message=Yii::t('yii','{attribute} must be greater than or equal to "{compareValue}".');
            
$condition='parseFloat(value)<parseFloat('.$compareValue.')';
            break;
        case 
'<':
            if(
$message===null)
                
$message=Yii::t('yii','{attribute} must be less than "{compareValue}".');
            
$condition='parseFloat(value)>=parseFloat('.$compareValue.')';
            break;
        case 
'<=':
            if(
$message===null)
                
$message=Yii::t('yii','{attribute} must be less than or equal to "{compareValue}".');
            
$condition='parseFloat(value)>parseFloat('.$compareValue.')';
            break;
        default:
            throw new 
CException(Yii::t('yii','Invalid operator "{operator}".',array('{operator}'=>$this->operator)));
    }

    
$message=strtr($message,array(
        
'{attribute}'=>$object->getAttributeLabel($attribute),
        
'{compareValue}'=>$compareTo,
    ));

    return 
"
if("
.($this->allowEmpty "$.trim(value)!='' && " '').$condition.") {
messages.push("
.CJSON::encode($message).");
}
"
;
}

返回执行客户端验证所需的JavaScript。

validateAttribute() 方法
protected void validateAttribute(CModel $object, string $attribute)
$object CModel 需要验证的对象
$attribute string 需要验证的属性
源码: framework/validators/CCompareValidator.php#81 (显示)
protected function validateAttribute($object,$attribute)
{
    
$value=$object->$attribute;
    if(
$this->allowEmpty && $this->isEmpty($value))
        return;
    if(
$this->compareValue!==null)
        
$compareTo=$compareValue=$this->compareValue;
    else
    {
        
$compareAttribute=$this->compareAttribute===null $attribute.'_repeat' $this->compareAttribute;
        
$compareValue=$object->$compareAttribute;
        
$compareTo=$object->getAttributeLabel($compareAttribute);
    }

    switch(
$this->operator)
    {
        case 
'=':
        case 
'==':
            if((
$this->strict && $value!==$compareValue) || (!$this->strict && $value!=$compareValue))
            {
                
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be repeated exactly.');
                
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo));
            }
            break;
        case 
'!=':
            if((
$this->strict && $value===$compareValue) || (!$this->strict && $value==$compareValue))
            {
                
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must not be equal to "{compareValue}".');
                
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
            }
            break;
        case 
'>':
            if(
$value<=$compareValue)
            {
                
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be greater than "{compareValue}".');
                
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
            }
            break;
        case 
'>=':
            if(
$value<$compareValue)
            {
                
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be greater than or equal to "{compareValue}".');
                
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
            }
            break;
        case 
'<':
            if(
$value>=$compareValue)
            {
                
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be less than "{compareValue}".');
                
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
            }
            break;
        case 
'<=':
            if(
$value>$compareValue)
            {
                
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be less than or equal to "{compareValue}".');
                
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
            }
            break;
        default:
            throw new 
CException(Yii::t('yii','Invalid operator "{operator}".',array('{operator}'=>$this->operator)));
    }
}

验证传入对象的属性。 如果验证到任何错误,错误消息将被添加到该对象中。

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.