CStringValidator

优质
小牛编辑
120浏览
2023-12-01
所有包 | 属性 | 方法
system.validators
继承class CStringValidator » CValidator » CComponent
源自1.0
版本$Id: CStringValidator.php 3491 2011-12-17 05:17:57Z jefftulsa $
源码framework/validators/CStringValidator.php
CStringValidator验证属性值有确定的长度。

注意:此验证器仅适用于字符串类型的属性。

除了使用message属性设置一个自定义错误提示信息之外, CStringValidator还有一对自定义错误提示信息,你可以针对不同 的验证情景模式分别使用。要为一个太短的数值指定自定义错误提 示信息,你可以使用tooBig属性。类似地有tooLong。 提示信息可以包含占位符,占位符可以被实际内容替换。除了所有 验证器均可识别的 "{attribute}" 占位符(参见CValidator)外, CStringValidator还允许指定下列占位符:
  • {min}: 当使用tooShort时,使用最短长度min替换。
  • {max}: 当使用tooLong时,使用最大长度max替换。
  • {length}: 当使用message时, 使用需要的确定的长度替换 is,如果已经设置。

公共属性

隐藏继承属性

属性类型描述定义在
allowEmptyboolean是否此属性值允许为null或empty。默认值是true, 表示如果此属性值为空时它将通过验证。CStringValidator
attributesarray需要被验证的属性的列表。CValidator
builtInValidatorsarray内置验证器列表 (name=>class)CValidator
enableClientValidationboolean是否执行客户端验证。默认值为true。 参见CActiveForm::enableClientValidation以了解更多关于客户端验证的细节。CValidator
encodingstring需要验证的字符串的编码(例如,“UTF-8”)。 仅当PHP扩展mstring启用时,这个值才会被使用。 这个值将被作为mb_strlen()函数的第二个参数被使用。 如果这个值没有设置,将使用应用程序的字符集 作为默认值。 如果这个值设置为false,那么不管mbstring是否启用,strlen()都将被使用。CStringValidator
isinteger确定的长度。默认值是null,表示没有确定的长度限制。CStringValidator
maxinteger最大长度。默认值是null,表示没有最大限制。CStringValidator
messagestring用户自定义的错误提示信息。不同的验证器可以在该信息中 定义各种占位符(将被实际值替换)。占位符“{attribute}”可以被所有 验证器识别,它会被使用属性的标签来替换。CValidator
mininteger最短长度。默认值是null,表示没有最短限制。CStringValidator
onarray验证器将被应用到的情景模式的列表。 数组的键-值都是情景模式的名称。CValidator
safeboolean进行整块赋值是是否考虑此验证器中列出的属性的安全性。 默认值为true。CValidator
skipOnErrorboolean如果当前属性已经存在验证错误,这个验证规则 是否跳过。默认值是false。CValidator
tooLongstring当长度太大时使用的用户自定义错误提示信息。CStringValidator
tooShortstring当长度太短时使用的用户自定义错误提示信息。CStringValidator

公共方法

隐藏继承方法

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

属性详细

allowEmpty 属性 public boolean $allowEmpty;

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

encoding 属性 (可用自 v1.1.1) public string $encoding;

需要验证的字符串的编码(例如,“UTF-8”)。 仅当PHP扩展mstring启用时,这个值才会被使用。 这个值将被作为mb_strlen()函数的第二个参数被使用。 如果这个值没有设置,将使用应用程序的字符集 作为默认值。 如果这个值设置为false,那么不管mbstring是否启用,strlen()都将被使用。

is 属性 public integer $is;

确定的长度。默认值是null,表示没有确定的长度限制。

max 属性 public integer $max;

最大长度。默认值是null,表示没有最大限制。

min 属性 public integer $min;

最短长度。默认值是null,表示没有最短限制。

tooLong 属性 public string $tooLong;

当长度太大时使用的用户自定义错误提示信息。

tooShort 属性 public string $tooShort;

当长度太短时使用的用户自定义错误提示信息。

方法详细

clientValidateAttribute() 方法 (可用自 v1.1.7)
public string clientValidateAttribute(CModel $object, string $attribute)
$objectCModel需要验证的对象
$attributestring需要验证的属性
{return}string客户端验证脚本
源码: framework/validators/CStringValidator.php#114 (显示) publicfunctionclientValidateAttribute($object,$attribute)
{
$label=$object->getAttributeLabel($attribute);

if(($message=$this->message)===null)
$message=Yii::t('yii','{attribute}isofthewronglength(shouldbe{length}characters).');
$message=strtr($message,array(
'{attribute}'=>$label,
'{length}'=>$this->is,
));

if(($tooShort=$this->tooShort)===null)
$tooShort=Yii::t('yii','{attribute}istooshort(minimumis{min}characters).');
$tooShort=strtr($tooShort,array(
'{attribute}'=>$label,
'{min}'=>$this->min,
));

if(($tooLong=$this->tooLong)===null)
$tooLong=Yii::t('yii','{attribute}istoolong(maximumis{max}characters).');
$tooLong=strtr($tooLong,array(
'{attribute}'=>$label,
'{max}'=>$this->max,
));

$js='';
if($this->min!==null)
{
$js.="
if(value.length<{$this->min}){
messages.push(".CJSON::encode($tooShort).");
}
";
}
if($this->max!==null)
{
$js.="
if(value.length>{$this->max}){
messages.push(".CJSON::encode($tooLong).");
}
";
}
if($this->is!==null)
{
$js.="
if(value.length!={$this->is}){
messages.push(".CJSON::encode($message).");
}
";
}

if($this->allowEmpty)
{
$js="
if($.trim(value)!=''){
$js
}
";
}

return$js;
}

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

参见

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

if(function_exists('mb_strlen')&&$this->encoding!==false)
$length=mb_strlen($value,$this->encoding?$this->encoding:Yii::app()->charset);
else
$length=strlen($value);

if($this->min!==null&&$length<$this->min)
{
$message=$this->tooShort!==null?$this->tooShort:Yii::t('yii','{attribute}istooshort(minimumis{min}characters).');
$this->addError($object,$attribute,$message,array('{min}'=>$this->min));
}
if($this->max!==null&&$length>$this->max)
{
$message=$this->tooLong!==null?$this->tooLong:Yii::t('yii','{attribute}istoolong(maximumis{max}characters).');
$this->addError($object,$attribute,$message,array('{max}'=>$this->max));
}
if($this->is!==null&&$length!==$this->is)
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute}isofthewronglength(shouldbe{length}characters).');
$this->addError($object,$attribute,$message,array('{length}'=>$this->is));
}
}

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