# Class yii\captcha\CaptchaValidator

Inheritance | yii\captcha\CaptchaValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/captcha/CaptchaValidator.php

CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.

CaptchaValidator should be used together with yii\captcha\CaptchaAction.

Note that once CAPTCHA validation succeeds, a new CAPTCHA will be generated automatically. As a result, CAPTCHA validation should not be used in AJAX validation mode because it may fail the validation even if a user enters the same code as shown in the CAPTCHA image which is actually different from the latest CAPTCHA code.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$attributeNames | array | Attribute names. This property is read-only. | yii\validators\Validator
$attributes | array|string | Attributes to be validated by this validator. | yii\validators\Validator
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$builtInValidators | array | List of built-in validators (name => class or configuration) | yii\validators\Validator
$captchaAction | string | The route of the controller action that renders the CAPTCHA image. | yii\captcha\CaptchaValidator
$caseSensitive | boolean | Whether the comparison is case sensitive. | yii\captcha\CaptchaValidator
$enableClientValidation | boolean | Whether to enable client-side validation for this validator. | yii\validators\Validator
$except | array|string | Scenarios that the validator should not be applied to. | yii\validators\Validator
$isEmpty | callable | A PHP callable that replaces the default implementation of isEmpty(). | yii\validators\Validator
$message | string | The user-defined error message. | yii\validators\Validator
$on | array|string | Scenarios that the validator can be applied to. | yii\validators\Validator
$skipOnEmpty | boolean | Whether to skip this validator if the input is empty. | yii\captcha\CaptchaValidator
$skipOnError | boolean | Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. | yii\validators\Validator
$validationAttributes | array | List of attribute names. This property is read-only. | yii\validators\Validator
$when | callable | A PHP callable whose return value determines whether this validator should be applied. | yii\validators\Validator
$whenClient | string | A JavaScript function name whose return value determines whether this validator should be applied on the client-side. | yii\validators\Validator

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__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() | Adds an error about the specified attribute to the model object. | 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() | Returns the JavaScript needed for performing client-side validation. | yii\captcha\CaptchaValidator
createCaptchaAction() | Creates the CAPTCHA action object from the route specified by $captchaAction. | yii\captcha\CaptchaValidator
createValidator() | Creates a validator object. | 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() | Returns cleaned attribute names without the `!` character at the beginning. | yii\validators\Validator
getBehavior() | Returns the named behavior object. | yii\base\Component
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component
getClientOptions() | Returns the client-side validation options. | yii\captcha\CaptchaValidator
getValidationAttributes() | Returns a list of attributes this validator applies to. | 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() | Returns a value indicating whether the validator is active for the given scenario and attribute. | yii\validators\Validator
isEmpty() | Checks if the given value is empty. | 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() | Validates a given value. | yii\validators\Validator
validateAttribute() | Validates a single attribute. | yii\validators\Validator
validateAttributes() | Validates the specified object. | yii\validators\Validator

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
formatMessage() | Formats a mesage using the I18N, or simple strtr if `\Yii::$app` is not available. | yii\validators\Validator
validateValue() | Validates a value. | yii\captcha\CaptchaValidator

## Property Details

$captchaAction public property

The route of the controller action that renders the CAPTCHA image.

public string $captchaAction = 'site/captcha'
$caseSensitive public property

Whether the comparison is case sensitive. Defaults to false.

public boolean $caseSensitive = false
$skipOnEmpty public property

Whether to skip this validator if the input is empty.

public boolean $skipOnEmpty = false

## Method Details

clientValidateAttribute() public method

Returns the JavaScript needed for performing client-side validation.

Calls getClientOptions() to generate options array for client-side validation.

You may override this method to return the JavaScript validation code if the validator can support client-side validation.

The following JavaScript variables are predefined and can be used in the validation code:

- `attribute`: an object describing the the attribute being validated.
- `value`: the value being validated.
- `messages`: an array used to hold the validation error messages for the attribute.
- `deferred`: an array used to hold deferred objects for asynchronous validation
- `$form`: a jQuery object containing the form element

The `attribute` object contains the following properties:

- `id`: a unique ID identifying the attribute (e.g. "loginform-username") in the form
- `name`: attribute name or expression (e.g. "[0]content" for tabular input)
- `container`: the jQuery selector of the container of the input field
- `input`: the jQuery selector of the input field under the context of the form
- `error`: the jQuery selector of the error tag under the context of the container
- `status`: status of the input field, 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating

public string|null clientValidateAttribute ( $model, $attribute, $view )
$model | yii\base\Model | The data model being validated
$attribute | string | The name of the attribute to be validated.
$view | yii\web\View | The view object that is going to be used to render views or view files containing a model form with this validator applied.
return | string|null | The client-side validation script. Null if the validator does not support client-side validation.

createCaptchaAction() public method

Creates the CAPTCHA action object from the route specified by $captchaAction.

public yii\captcha\CaptchaAction createCaptchaAction ( )
return | yii\captcha\CaptchaAction | The action object
throws | yii\base\InvalidConfigException |

getClientOptions() public method (available since version 2.0.11)

Returns the client-side validation options.

This method is usually called from clientValidateAttribute(). You may override this method to modify options that will be passed to the client-side validation.

public array getClientOptions ( $model, $attribute )
$model | yii\base\Model | The model being validated
$attribute | string | The attribute name being validated
return | array | The client-side validation options

init() public method

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 ( )

validateValue() protected method

Validates a value.

A validator class can implement this method to support data validation out of the context of a data model.

protected array|null validateValue ( $value )
$value | mixed | The data value to be validated.
return | array|null | The error message and the array of parameters to be inserted into the error message. ```php if (!$valid) {
    return [$this->message, [
        'param1' => $this->param1,
        'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit()),
        'mimeTypes' => implode(', ', $this->mimeTypes),
        'param4' => 'etc...',
    ]];
} return null; ``` for this example `message` template can contain `{param1}`, `{formattedLimit}`, `{mimeTypes}`, `{param4}` Null should be returned if the data is valid.
throws | yii\base\NotSupportedException | if the validator does not supporting data validation without a model