CRangeValidator

优质
小牛编辑
126浏览
2023-12-01
所有包 | 属性 | 方法
system.validators
继承class CRangeValidator » CValidator » CComponent
源自1.0
版本$Id: CRangeValidator.php 3120 2011-03-25 01:50:48Z qiang.xue $
源码framework/validators/CRangeValidator.php
CRangeValidator验证此属性值在列表之中(通过range指定)。 通过使用not属性,你也可以实现相反的验证逻辑(从1.1.5版开始可用)。

公共属性

隐藏继承属性

属性类型描述定义在
allowEmptyboolean是否此属性值可以为null或empty。默认值是true, 表示如果此属性值为空时将通过验证。CRangeValidator
attributesarray需要被验证的属性的列表。CValidator
builtInValidatorsarray内置验证器列表 (name=>class)CValidator
enableClientValidationboolean是否执行客户端验证。默认值为true。 参见CActiveForm::enableClientValidation以了解更多关于客户端验证的细节。CValidator
messagestring用户自定义的错误提示信息。不同的验证器可以在该信息中 定义各种占位符(将被实际值替换)。占位符“{attribute}”可以被所有 验证器识别,它会被使用属性的标签来替换。CValidator
notboolean是否反转验证逻辑。默认值是false。如果设置为true, 此属性值不在列表(通过range定义)中时验证通过。CRangeValidator
onarray验证器将被应用到的情景模式的列表。 数组的键-值都是情景模式的名称。CValidator
rangearray此属性值的有效值列表CRangeValidator
safeboolean进行整块赋值是是否考虑此验证器中列出的属性的安全性。 默认值为true。CValidator
skipOnErrorboolean如果当前属性已经存在验证错误,这个验证规则 是否跳过。默认值是false。CValidator
strictboolean是否执行严格比较(类型与值都必须一致)。默认值是false。CRangeValidator

公共方法

隐藏继承方法

方法描述定义在
__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。CRangeValidator
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()验证传入对象的属性。CRangeValidator

属性详细

allowEmpty 属性 public boolean $allowEmpty;

是否此属性值可以为null或empty。默认值是true, 表示如果此属性值为空时将通过验证。

not 属性 (可用自 v1.1.5) public boolean $not;

是否反转验证逻辑。默认值是false。如果设置为true, 此属性值不在列表(通过range定义)中时验证通过。

range 属性 public array $range;

此属性值的有效值列表

strict 属性 public boolean $strict;

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

方法详细

clientValidateAttribute() 方法 (可用自 v1.1.7)
public string clientValidateAttribute(CModel $object, string $attribute)
$objectCModel需要验证的对象
$attributestring需要验证的属性
{return}string客户端验证脚本
源码: framework/validators/CRangeValidator.php#75 (显示) publicfunctionclientValidateAttribute($object,$attribute)
{
if(!is_array($this->range))
thrownewCException(Yii::t('yii','The"range"propertymustbespecifiedwithalistofvalues.'));

if(($message=$this->message)===null)
$message=$this->not?Yii::t('yii','{attribute}isinthelist.'):Yii::t('yii','{attribute}isnotinthelist.');
$message=strtr($message,array(
'{attribute}'=>$object->getAttributeLabel($attribute),
));

$range=array();
foreach($this->rangeas$value)
$range[]=(string)$value;
$range=CJSON::encode($range);

return"
if(".($this->allowEmpty?"$.trim(value)!=''&&":'').($this->not?"$.inArray(value,$range)>=0":"$.inArray(value,$range)<0")."){
messages.push(".CJSON::encode($message).");
}
";
}

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

参见

  • CActiveForm::enableClientValidation
validateAttribute() 方法
protected void validateAttribute(CModel $object, string $attribute)
$objectCModel需要验证的对象
$attributestring需要验证的属性
源码: framework/validators/CRangeValidator.php#48 (显示) protectedfunctionvalidateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty&&$this->isEmpty($value))
return;
if(!is_array($this->range))
thrownewCException(Yii::t('yii','The"range"propertymustbespecifiedwithalistofvalues.'));
if(!$this->not&&!in_array($value,$this->range,$this->strict))
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute}isnotinthelist.');
$this->addError($object,$attribute,$message);
}
elseif($this->not&&in_array($value,$this->range,$this->strict))
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute}isinthelist.');
$this->addError($object,$attribute,$message);
}
}

验证传入对象的属性。 如果存在任何验证错误,错误提示信息将被添加到此对象中。