Class yii\captcha\CaptchaValidator
CaptchaValidator 验证属性值是否与 CAPTCHA 中显示的验证码相同。
CaptchaValidator 应与 yii\captcha\CaptchaAction 一起使用。
请注意,一旦 CAPTCHA 验证成功,将自动生成新的 CAPTCHA。 因此,CAPTCHA 验证不应在AJAX验证模式下使用,因为即使用户输入的 CAPTCHA 图像中显示的代码与最新的 CAPTCHA 代码实际不同,它也可能无法通过验证。
公共属性
| 属性 | 类型 | 描述 | 被定义在 | 
|---|---|---|---|
| $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 | 
| $captchaAction | string | 渲染 CAPTCHA 图像的控制器动作的路径。 | yii\captcha\CaptchaValidator | 
| $caseSensitive | boolean | 比较是否区分大小写。默认为 false。 | yii\captcha\CaptchaValidator | 
| $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}: 被校验的属性值
注意:有一些校验器会引入其他的属性用于指定的校验条件未满足时的错误消息。
关于这些属性的具体详情,请参考具体的类 API 文档。
通常,
这些属性代表最重要的校验规则未满足时的所触发的主要错误消息。 | yii\validators\Validator | 
| $on | array|string | 校验器被应用的情景。 对于多情景,请以一个数组的形式指定它们。对于单情景,你可以使用一个字符串或者一个数组。 | yii\validators\Validator | 
| $skipOnEmpty | boolean | 如果输入为空,是否跳过此验证器。 | yii\captcha\CaptchaValidator | 
| $skipOnError | boolean | 当被校验的属性根据之前的校验规则已经有一些校验错误时,这个校验规则是否应该被跳过。 默认为 true。 | yii\validators\Validator | 
| $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\captcha\CaptchaValidator | 
| createCaptchaAction() | 从 $captchaAction 指定的路径创建 CAPTCHA 动作对象。 | yii\captcha\CaptchaValidator | 
| 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\captcha\CaptchaValidator | 
| 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\captcha\CaptchaValidator | 
| 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\Validator | 
| validateAttributes() | 校验指定的对象。 | yii\validators\Validator | 
受保护的方法
| 方法 | 描述 | 被定义在 | 
|---|---|---|
| formatMessage() | 使用 I18N 格式化消息,或者只是简单的 strtr ,如果 \Yii::$app不可用的话。 | yii\validators\Validator | 
| validateValue() | 校验一个值。 一个校验类可以实现这个方法,以支持在数据模型上下文之外的地方支持数据校验。 | yii\captcha\CaptchaValidator | 
属性详情
渲染 CAPTCHA 图像的控制器动作的路径。
比较是否区分大小写。默认为 false。
如果输入为空,是否跳过此验证器。
方法详情
返回可用于客户端校验的 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。 客户端校验。 | 
|---|---|---|
从 $captchaAction 指定的路径创建 CAPTCHA 动作对象。
| public yii\captcha\CaptchaAction createCaptchaAction() | ||
| return | yii\captcha\CaptchaAction | 动作对象 | 
|---|---|---|
| throws | yii\base\InvalidConfigException | |
返回客户端校验参数。 这个方法通常在 clientValidateAttribute() 中调用。 你可以改写这个方法用于修改传给客户端校验的参数。
| public array getClientOptions($model, $attribute) | ||
| $model | yii\base\Model | 被校验的模型。 | 
| $attribute | string | 被校验的属性名称。 | 
| return | array | 客户端校验参数。 | 
|---|---|---|
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() | 
校验一个值。 一个校验类可以实现这个方法,以支持在数据模型上下文之外的地方支持数据校验。
| protected array|null validateValue($value) | ||
| $value | mixed | 被校验的数据值。 | 
| return | array|null | 错误消息,和可用于替换错误消息中占位符的参数。 对于这个例子  如果数据是合法的,返回 null。 | 
|---|---|---|
| throws | yii\base\NotSupportedException | 如果校验器不支持模型外数据校验。 |