没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\validators\CompareValidator

继承yii\validators\CompareValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/validators/CompareValidator.php

CompareValidator 用于将指定的属性的值和其他值进行比较。

被比较的值可以是其他的属性的值(通过 $compareAttribute 字段设置), 也可以是一个常量( 通过 $compareValue 设置)。 如果两个都设置了,后者优先。 如果都没有设置, 这个属性将会跟其他以当前属性名为前缀, 以 "_repeat" 为后缀的属性进行比较。

CompareValidator 支持不同的比较运算符, 通过 $operator 属性指定。

默认的比较功能是基于字符串值,即待比较的值将以字节的形式进行比较。 当比较数值型时,确认已将 $type 属性的值设置为 TYPE_NUMBER 以启用数值比较。

公共属性

隐藏继承的属性

属性类型描述被定义在
$attributeNames array 属性名称列表. yii\validators\Validator
$attributes array|string 将要被这个校验器校验的属性名,或者列表。如果多属性,请通过一个数组设置它们。 对于单属性,你可以使用一个字符串,也可以使用一个数组指定。 yii\validators\Validator
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$builtInValidators array List of built-in validators (name => class or configuration) yii\validators\Validator
$compareAttribute string 待比较的属性值。当它和 $compareValue 都被设置时,后者优先。 如果都没有设置,它会跟以当前属性名后缀 "_repeat" 的属性进行比较。 例如: 如果当前比较的是 'password' 属性, 那么它比较的属性将为 'password_repeat' yii\validators\CompareValidator
$compareValue mixed 待比较的常量值。 当这个属性和 $compareAttribute 同时设置时,这个属性优先。 yii\validators\CompareValidator
$enableClientValidation boolean 是否启用这个校验器的客户端校验。 实际的校验过程是通过 clientValidateAttribute() 返回的 JS 代码来执行。 如果这个方法返回 null, 即使这个属性值为 true ,也不会执行任何客户端校验。 yii\validators\Validator
$except array|string 校验器不应该应用的情景。 对于多情景,请以一个数组的形式指定它们。对于单情景,你可以使用一个字符串或者一个数组。 yii\validators\Validator
$isEmpty callable 用于替换默认的 isEmpty() 空值校验方法, 如果没有设置,将会使用 isEmpty() 做空值校验。 这个函数的声明应该为 function ($value), 它的返回值为一个代表这个值是否为空的布尔值。 yii\validators\Validator
$message string 用户定义错误消息。 它可包含如下的占位符,并将被校验器自动的替换: - {attribute}: 待校验的属性标签名 - {value}: 待校验的属性值 - {compareValue}: 比较的值或者属性标签 - {compareAttribute}: 比较的属性标签 - {compareValueOrAttribute}: 比较的值或者属性标签 yii\validators\CompareValidator
$on array|string 校验器被应用的情景。 对于多情景,请以一个数组的形式指定它们。对于单情景,你可以使用一个字符串或者一个数组。 yii\validators\Validator
$operator string 比较运算符,支持如下运算符: - ==: 比较两个值是否相等。用于非严格模式比较。 - ===: 比较两个值是否全等。用于严格模式比较。 - !=: 比较两个值是否不相等。用于非严格模式比较。 - !==: 比较两个值是否不全等。用于严格模式比较。 - >: 检测待校验的值是否大于待比较的值。 - >=: 检测待校验的值是否大于等于待比较的值。 - <: 检测待校验的值是否小于待比较的值。 - <=: 检测待校验的值是否小于等于待比较的值。 如果你想比较数值,确保将 $type 属性设置为 number yii\validators\CompareValidator
$skipOnEmpty boolean 当被校验的属性值为 null 或者空字符串时, 是否该跳过这个校验规则。 yii\validators\Validator
$skipOnError boolean 当被校验的属性根据之前的校验规则已经有一些校验错误时,这个校验规则是否应该被跳过。 默认为 true。 yii\validators\Validator
$type string 被比较的值类型名称。支持以下类型: - string: 待比较的值是字符串型。在比较前不会做类型转换。 - number: 待比较的值是数值型。字符串值在比较前会被转换为数值。 yii\validators\CompareValidator
$validationAttributes yii\validators\Validator
$when callable 一个PHP函数调用,它的返回值将会决定这个校验器是否被应用。 这个函数的声明应该为 function ($model, $attribute) ,其中 $model$attribute 代表被校验的模型和属性。 这个函数应该返回一个布尔值。 这个属性主要用于支持服务端条件校验。 如果这个属性没有被设置,这个校验器将会总是在服务端执行校验。 以下是一个示例,只有当前选择的国家为 USA 时,才会在服务端执行此校验器: `php function ($model) { return $model->country == Country::USA; } ` yii\validators\Validator
$whenClient string 这是一个 JS 函数,它的返回值将会决定校验器是否在客户端执行。 这个函数的声明应该为 function (attribute, value) , 其中 attribute 代表被要被校验的属性对象(参考: clientValidateAttribute()), value 是属性当前值。 这个属性主要用于支持客户端条件校验。 如果这个属性没有被设置,这个校验器将会总是在客户端执行校验。 以下是一个示例,只有当前选择的国家为 USA 时,才会在客户端执行此校验器: `javascript function (attribute, value) { return $('#country'). yii\validators\Validator

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addError() 添加指定属性的错误到模型对象中。 这是一个帮助方法用于执行消息的选择和国际化。 yii\validators\Validator
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
clientValidateAttribute() 返回可用于客户端校验的 JS 代码。 yii\validators\CompareValidator
createValidator() 创建校验器对象。 yii\validators\Validator
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getAttributeNames() 返回去除开头 ! 的属性名称。 yii\validators\Validator
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getClientOptions() 返回客户端校验参数。 这个方法通常在 clientValidateAttribute() 中调用。 你可以改写这个方法用于修改传给客户端校验的参数。 yii\validators\CompareValidator
getValidationAttributes() yii\validators\Validator
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. yii\validators\CompareValidator
isActive() 返回一个值代表当前校验器在当前情景和属性下是否是激活状态。 yii\validators\Validator
isEmpty() 检测指定的值是否为空。 如果它是一个 null,一个空的数组,或者一个空字符串,那么它会被认为是一个空值。 注意这个方法和 PHP 的 empty() 函数不同,当值为 0 时,它会返还 false 。(非空值) yii\validators\Validator
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
trigger() Triggers an event. yii\base\Component
validate() 校验一个指定的值。 你可以使用这个方法在数据模型上下文之外的地方校验一个值。 yii\validators\Validator
validateAttribute() 校验单个属性。 子类必须实现这个方法以实现具体的校验逻辑。 yii\validators\CompareValidator
validateAttributes() 校验指定的对象。 yii\validators\Validator

受保护的方法

隐藏继承的方法

方法描述被定义在
compareValues() 使用指定的比较运算符比较两个值。 yii\validators\CompareValidator
formatMessage() 使用 I18N 格式化消息,或者只是简单的 strtr ,如果 \Yii::$app 不可用的话。 yii\validators\Validator
validateValue() 校验一个值。 一个校验类可以实现这个方法,以支持在数据模型上下文之外的地方支持数据校验。 yii\validators\CompareValidator

常量

隐藏继承的常量

常量描述被定义在
TYPE_NUMBER 'number' 用于指定属性 $type 以数值型比较的常量。 yii\validators\CompareValidator
TYPE_STRING 'string' 用于指定属性 $type 以字符串型比较的常量。 yii\validators\CompareValidator

属性详情

$compareAttribute 公共 属性

待比较的属性值。当它和 $compareValue 都被设置时,后者优先。 如果都没有设置,它会跟以当前属性名后缀 "_repeat" 的属性进行比较。 例如: 如果当前比较的是 'password' 属性, 那么它比较的属性将为 'password_repeat'

参见 $compareValue.

public string $compareAttribute null
$compareValue 公共 属性

待比较的常量值。 当这个属性和 $compareAttribute 同时设置时,这个属性优先。

参见 $compareAttribute.

public mixed $compareValue null
$message 公共 属性

用户定义错误消息。 它可包含如下的占位符,并将被校验器自动的替换:

  • {attribute}: 待校验的属性标签名
  • {value}: 待校验的属性值
  • {compareValue}: 比较的值或者属性标签
  • {compareAttribute}: 比较的属性标签
  • {compareValueOrAttribute}: 比较的值或者属性标签
public string $message null
$operator 公共 属性

比较运算符,支持如下运算符:

  • ==: 比较两个值是否相等。用于非严格模式比较。
  • ===: 比较两个值是否全等。用于严格模式比较。
  • !=: 比较两个值是否不相等。用于非严格模式比较。
  • !==: 比较两个值是否不全等。用于严格模式比较。
  • >: 检测待校验的值是否大于待比较的值。
  • >=: 检测待校验的值是否大于等于待比较的值。
  • <: 检测待校验的值是否小于待比较的值。
  • <=: 检测待校验的值是否小于等于待比较的值。

如果你想比较数值,确保将 $type 属性设置为 number

public string $operator '=='
$type 公共 属性

被比较的值类型名称。支持以下类型:

public string $type self::TYPE_STRING

方法详情

clientValidateAttribute() 公共 方法

返回可用于客户端校验的 JS 代码。

调用 getClientOptions() 来生成客户端校验数组。

如果这个校验器可以支持客户端校验的话, 你可以重写这个方法来返回 JS 校验代码。

如下预定义 JS 变量可以用于校验代码中:

  • attribute: 描述被校验属性的对象
  • value: 被校验的值
  • messages: 保存属性校验错误消息的数组
  • deferred: 保存 deferred 对象的数组用于异步执行校验
  • $form: jQuery 对象用于保存表单元素

attribute 包含如下属性:

  • id: 唯一 ID 用于在表单中标识这个属性(例如: "loginform-username")
  • name: 属性名称或表达式(例如:表单输入时,可以为 "[0]content" )
  • container: 输入容器的 jQuery 选择器
  • input: 表单上下文输入字段的 jQuery 选择器
  • error: 容器上下文错误标签的 jQuery 选择器
  • status: 输入字段的状态,0:空的,没有输入,1:校验过了,2:待校验,3:校验中
public string|null clientValidateAttribute($model, $attribute, $view)
$model yii\base\Model

被校验的数据模型。

$attribute string

待校验的属性名称

$view yii\web\View

将要被用于渲染视图或者视图文件的视图对象 包含应用校验器的表单模型。

return string|null

客户端校验脚本。如果校验器不支持的话,返回 null。 客户端校验。

compareValues() 受保护 方法

使用指定的比较运算符比较两个值。

protected boolean compareValues($operator, $type, $value, $compareValue)
$operator string

比较运算符

$type string

待比较值类型

$value mixed

待比较值

$compareValue mixed

被比较的值

return boolean

使用指定的运算符比较返回值是否为真

getClientOptions() 公共 方法

返回客户端校验参数。 这个方法通常在 clientValidateAttribute() 中调用。 你可以改写这个方法用于修改传给客户端校验的参数。

public array getClientOptions($model, $attribute)
$model yii\base\Model

被校验的模型。

$attribute string

被校验的属性名称。

return array

客户端校验参数。

init() 公共 方法

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init()
validateAttribute() 公共 方法

校验单个属性。 子类必须实现这个方法以实现具体的校验逻辑。

public void validateAttribute($model, $attribute)
$model yii\base\Model

被校验的数据模型对象。

$attribute string

被校验的属性名称。

validateValue() 受保护 方法

校验一个值。 一个校验类可以实现这个方法,以支持在数据模型上下文之外的地方支持数据校验。

protected array|null validateValue($value)
$value mixed

被校验的数据值。

return array|null

错误消息,和可用于替换错误消息中占位符的参数。

if (!$valid) {
    return [$this->message, [
        'param1' => $this->param1,
        'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit()),
        'mimeTypes' => implode(', ', $this->mimeTypes),
        'param4' => 'etc...',
    ]];
}

return null;

对于这个例子 message 模板可以包含 {param1}, {formattedLimit}, {mimeTypes}, {param4}

如果数据是合法的,返回 null。

throws yii\base\NotSupportedException

如果校验器不支持模型外数据校验。