CRegularExpressionValidator

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

公共属性

隐藏继承属性

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

公共方法

隐藏继承方法

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

属性详细

allowEmpty 属性 public boolean $allowEmpty;

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

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

是否反转验证逻辑。默认值是false。如果设置为true, 此属性值不匹配正则表达式(通过pattern定义)时验证通过。

pattern 属性 public string $pattern;

需要匹配的正则表达式

方法详细

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

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

$pattern=$this->pattern;
$pattern=preg_replace('/\\x{?([0-9a-fA-F]+)}?/','u$1',$pattern);
$delim=substr($pattern,0,1);
$endpos=strrpos($pattern,$delim,1);
$flag=substr($pattern,$endpos+1);
if($delim!=='/')
$pattern='/'.str_replace('/','\/',substr($pattern,1,$endpos-1)).'/';
else
$pattern=substr($pattern,0,$endpos+1);
if(!empty($flag))
$pattern.=preg_replace('/[^igm]/','',$flag);

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

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

参见

  • CActiveForm::enableClientValidation
validateAttribute() 方法
protected void validateAttribute(CModel $object, string $attribute)
$objectCModel需要验证的对象
$attributestring需要验证的属性
源码: framework/validators/CRegularExpressionValidator.php#44 (显示) protectedfunctionvalidateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty&&$this->isEmpty($value))
return;
if($this->pattern===null)
thrownewCException(Yii::t('yii','The"pattern"propertymustbespecifiedwithavalidregularexpression.'));
if((!$this->not&&!preg_match($this->pattern,$value))||($this->not&&preg_match($this->pattern,$value)))
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute}isinvalid.');
$this->addError($object,$attribute,$message);
}
}

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