CTypeValidator

优质
小牛编辑
126浏览
2023-12-01
所有包 | 属性 | 方法
system.validators
继承class CTypeValidator » CValidator » CComponent
源自1.0
版本$Id: CTypeValidator.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/validators/CTypeValidator.php
CTypeValidator验证属性的类型是否为type所指定的类型.

以下数据类型受到支持:
  • integer有符号整数。
  • float双精度浮点数。
  • string字符串。
  • array数组。
  • date日期型。
  • time时间型。
  • datetime日期时间型。


对于date类型,需要使用dateFormat 来确定日期字符串的解析方式。如果给定的日期值与预期 的格式不匹配,此属性将不能通过验证。

从1.1.7版开始,我们有了一个专用的日期验证器CDateValidator, 请考虑使用该验证器来验证一个日期类型的值。

当使用message属性定义一个自定义错误提示信息时, 可以在其中包含额外的占位符,占位符可以被实际内容替换。除了所有 验证器均可识别的 "{attribute}" 占位符(参见CValidator)外, CTypeValidator还允许指定下列占位符:
  • {type}: 使用属性所期望的类型type替换。

公共属性

隐藏继承属性

属性类型描述定义在
allowEmptyboolean是否此属性值允许为null或empty。默认值是true, 表示如果此属性值为空时它将通过验证。CTypeValidator
attributesarray需要被验证的属性的列表。CValidator
builtInValidatorsarray内置验证器列表 (name=>class)CValidator
dateFormatstring匹配日期格式的模板。默认值是 'MM/dd/yyyy'。 请参见CDateTimeParser以了解如何指定一个日期格式的更多细节。 仅当type设置为"date"时,这个值才会生效。CTypeValidator
datetimeFormatstring匹配日期时间格式的模板。默认值是 'MM/dd/yyyy hh:mm'。 请参见CDateTimeParser以了解如何指定一个日期时间格式的更多细节。 仅当type设置为"datetime"时,这个值才会生效。CTypeValidator
enableClientValidationboolean是否执行客户端验证。默认值为true。 参见CActiveForm::enableClientValidation以了解更多关于客户端验证的细节。CValidator
messagestring用户自定义的错误提示信息。不同的验证器可以在该信息中 定义各种占位符(将被实际值替换)。占位符“{attribute}”可以被所有 验证器识别,它会被使用属性的标签来替换。CValidator
onarray验证器将被应用到的情景模式的列表。 数组的键-值都是情景模式的名称。CValidator
safeboolean进行整块赋值是是否考虑此验证器中列出的属性的安全性。 默认值为true。CValidator
skipOnErrorboolean如果当前属性已经存在验证错误,这个验证规则 是否跳过。默认值是false。CValidator
timeFormatstring匹配日期格式的模板。默认值是 'hh:mm'。 请参见CDateTimeParser以了解如何指定一个时间格式的更多细节。 仅当type设置为"time"时,这个值才会生效。CTypeValidator
typestring属性值期望的数据类型。默认值是“string”。 有效的值包括 'string', 'integer', 'float', 'array', 'date', 'time' 和 'datetime'。CTypeValidator

公共方法

隐藏继承方法

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

属性详细

allowEmpty 属性 public boolean $allowEmpty;

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

dateFormat 属性 public string $dateFormat;

匹配日期格式的模板。默认值是 'MM/dd/yyyy'。 请参见CDateTimeParser以了解如何指定一个日期格式的更多细节。 仅当type设置为"date"时,这个值才会生效。

datetimeFormat 属性 public string $datetimeFormat;

匹配日期时间格式的模板。默认值是 'MM/dd/yyyy hh:mm'。 请参见CDateTimeParser以了解如何指定一个日期时间格式的更多细节。 仅当type设置为"datetime"时,这个值才会生效。

timeFormat 属性 public string $timeFormat;

匹配日期格式的模板。默认值是 'hh:mm'。 请参见CDateTimeParser以了解如何指定一个时间格式的更多细节。 仅当type设置为"time"时,这个值才会生效。

type 属性 public string $type;

属性值期望的数据类型。默认值是“string”。 有效的值包括 'string', 'integer', 'float', 'array', 'date', 'time' 和 'datetime'。

方法详细

validateAttribute() 方法
protected void validateAttribute(CModel $object, string $attribute)
$objectCModel需要验证的对象
$attributestring需要验证的属性
源码: framework/validators/CTypeValidator.php#82 (显示) protectedfunctionvalidateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty&&$this->isEmpty($value))
return;

if($this->type==='integer')
$valid=preg_match('/^[-+]?[0-9]+$/',trim($value));
elseif($this->type==='float')
$valid=preg_match('/^[-+]?([0-9]*.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value));
elseif($this->type==='date')
$valid=CDateTimeParser::parse($value,$this->dateFormat,array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0))!==false;
elseif($this->type==='time')
$valid=CDateTimeParser::parse($value,$this->timeFormat)!==false;
elseif($this->type==='datetime')
$valid=CDateTimeParser::parse($value,$this->datetimeFormat,array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0))!==false;
elseif($this->type==='array')
$valid=is_array($value);
else
return;

if(!$valid)
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute}mustbe{type}.');
$this->addError($object,$attribute,$message,array('{type}'=>$this->type));
}
}

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